Skip to content

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

TypeScript

fix(mcp-host): sweep session retention in bounded worker operations

Alfredo Lopeze38c9e4c3948
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index b7d1ee9a8..d8597a464 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.578",
+ "version": "0.1.579",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.578",
+ "version": "0.1.579",
"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 bf210a010..9ec275eab 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.578",
+ "version": "0.1.579",
"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/sessionSearch/__tests__/sessionSearch.test.ts b/mcp-host/src/core/sessionSearch/__tests__/sessionSearch.test.ts
index fc5e1d63c..f30821606 100644
--- a/mcp-host/src/core/sessionSearch/__tests__/sessionSearch.test.ts
+++ b/mcp-host/src/core/sessionSearch/__tests__/sessionSearch.test.ts
@@ -411,6 +411,61 @@ describe('SessionSearchService — retention sweep', () => {
expect(first).toBe(1)
expect(second).toBe(0)
})
+
+ it('#13 sweeps a backlog in bounded worker operations', async () => {
+ const nowSec = Date.now() / 1000
+ for (const id of ['old-a', 'old-b', 'old-c']) {
+ seedSession(h.db, {
+ id,
+ userId: 'u1',
+ endReason: 'compression',
+ endedAt: nowSec - 100 * 86400,
+ })
+ for (let ordinal = 0; ordinal < 300; ordinal++) {
+ seedMessage(h.db, { sessionId: id, ordinal, role: 'user', content: 'expiredmarker' })
+ }
+ }
+ seedSession(h.db, {
+ id: 'recent-closed',
+ userId: 'u1',
+ endReason: 'user_closed',
+ endedAt: nowSec - 30 * 86400,
+ })
+ seedMessage(h.db, {
+ sessionId: 'recent-closed',
+ ordinal: 0,
+ role: 'user',
+ content: 'keptmarker',
+ })
+ seedSession(h.db, { id: 'inflight', userId: 'u1', endReason: null, endedAt: null })
+
+ const enqueueSync = vi.spyOn(h.persistQueue, 'enqueueSync')
+ const deleted = await h.service.sweepRetention(90)
+ expect(deleted).toBe(3)
+
+ const sweeps = enqueueSync.mock.calls.filter(([op]) => op.kind === 'sweep_closed_sessions')
+ // 900 messages at 250 per op, then the empty sessions, then the empty batch.
+ expect(sweeps.length).toBeGreaterThanOrEqual(5)
+ expect(enqueueSync.mock.results).toHaveLength(sweeps.length)
+ for (const result of enqueueSync.mock.results) {
+ const batch = (await result.value) as { deleted_messages: number; deleted_sessions: number }
+ expect(batch.deleted_messages).toBeLessThanOrEqual(250)
+ expect(batch.deleted_sessions).toBeLessThanOrEqual(250)
+ }
+
+ const remaining = h.db.prepare('SELECT id FROM sessions ORDER BY id').all() as Array<{
+ id: string
+ }>
+ expect(remaining.map(r => r.id)).toEqual(['inflight', 'recent-closed'])
+ const swept = h.db
+ .prepare("SELECT rowid FROM messages_fts WHERE messages_fts MATCH 'expiredmarker'")
+ .all()
+ expect(swept).toHaveLength(0)
+ const kept = h.db
+ .prepare("SELECT rowid FROM messages_fts WHERE messages_fts MATCH 'keptmarker'")
+ .all()
+ expect(kept).toHaveLength(1)
+ })
})
describe('Schema invariants', () => {
diff --git a/mcp-host/src/core/sessionSearch/__tests__/sweepRetention.realWorker.test.ts b/mcp-host/src/core/sessionSearch/__tests__/sweepRetention.realWorker.test.ts
new file mode 100644
index 000000000..106cc98b9
--- /dev/null
+++ b/mcp-host/src/core/sessionSearch/__tests__/sweepRetention.realWorker.test.ts
@@ -0,0 +1,117 @@
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
+import Database from 'better-sqlite3'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+import { Worker } from 'node:worker_threads'
+import { runMigrations } from '../../../db/migrate'
+import { applyPragmas } from '../../../db/pragmas'
+import { MCP_HOST_ROOT, compileSources } from '../../../db/worker/__tests__/compileSources'
+import { PersistQueue } from '../../conversation/persistence/persistQueue'
+import { SessionSearchService } from '../service'
+
+// A retention backlog whose single DELETE outlasts the progress bound used to
+// latch the queue (`db worker made no progress`), end the Host with exit 1
+// and repeat on every restart. The sweep runs on the real worker thread here,
+// so the bound is measured against real SQLite cascade and FTS work.
+const SESSIONS = 15_000
+const MESSAGES_PER_SESSION = 20
+const STALL_TIMEOUT_MS = 1000
+
+let tmpRoot: string
+let workerScript: string
+let dbPath: string
+
+function seedBacklog(file: string): void {
+ const db = new Database(file)
+ try {
+ applyPragmas(db)
+ runMigrations(db)
+ const endedAt = Date.now() / 1000 - 100 * 86400
+ const insertSession = db.prepare(
+ `INSERT INTO sessions (id, session_key, source, user_id, channel_type, channel_id,
+ started_at, ended_at, end_reason, message_count, tool_call_count,
+ input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, state)
+ VALUES (@id, @id, 'rpc', 'u1', 'rpc', 'agent', @ended_at, @ended_at, 'compression',
+ 0, 0, 0, 0, 0, 0, 'idle')`
+ )
+ const insertMessage = db.prepare(
+ `INSERT INTO messages (session_id, ordinal, role, content, timestamp, is_error, turn_number)
+ VALUES (@session_id, @ordinal, 'user', @content, @ended_at, 0, 1)`
+ )
+ db.transaction(() => {
+ for (let s = 0; s < SESSIONS; s++) {
+ const id = `expired-${s}`
+ insertSession.run({ id, ended_at: endedAt })
+ for (let ordinal = 0; ordinal < MESSAGES_PER_SESSION; ordinal++) {
+ insertMessage.run({
+ session_id: id,
+ ordinal,
+ content: `backlog message ${s} ${ordinal}`,
+ ended_at: endedAt,
+ })
+ }
+ }
+ })()
+ } finally {
+ db.close()
+ }
+}
+
+beforeAll(() => {
+ tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'sweep-real-worker-'))
+ fs.symlinkSync(path.join(MCP_HOST_ROOT, 'node_modules'), path.join(tmpRoot, 'node_modules'))
+ ;[workerScript] = compileSources(['db/worker/dbWorker.ts'], path.join(tmpRoot, 'dist'))
+ dbPath = path.join(tmpRoot, 'state.db')
+ seedBacklog(dbPath)
+}, 120_000)
+
+afterAll(() => {
+ if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true })
+})
+
+describe('sweepRetention on the real DB worker', () => {
+ it('sweeps a backlog longer than the progress bound without a stall', async () => {
+ const worker = new Worker(workerScript, {
+ workerData: { dbPath, checkpointEveryWrites: 100, heartbeatMs: 60_000 },
+ })
+ const failures: Error[] = []
+ const queue = new PersistQueue(worker, {
+ syncTimeoutMs: 900,
+ asyncTimeoutMs: 900,
+ stallTimeoutMs: STALL_TIMEOUT_MS,
+ onFailure: err => failures.push(err),
+ })
+ try {
+ await queue.awaitOpen()
+ const service = new SessionSearchService({ persistQueue: queue })
+ const enqueueSync = vi.spyOn(queue, 'enqueueSync')
+
+ const startedAt = Date.now()
+ const deleted = await service.sweepRetention(90)
+ const elapsedMs = Date.now() - startedAt
+
+ expect(failures).toEqual([])
+ expect(deleted).toBe(SESSIONS)
+ // Witnesses that the bound was exercised: the whole sweep took longer
+ // than one stall window, and it was split into many operations.
+ expect(elapsedMs).toBeGreaterThan(STALL_TIMEOUT_MS)
+ expect(enqueueSync.mock.calls.length).toBeGreaterThan(1)
+ await expect(queue.enqueueSync({ kind: 'ping' })).resolves.toBeDefined()
+ } finally {
+ await queue.close()
+ }
+
+ const reader = new Database(dbPath, { readonly: true })
+ try {
+ const counts = reader
+ .prepare(
+ 'SELECT (SELECT COUNT(*) FROM sessions) AS s, (SELECT COUNT(*) FROM messages) AS m'
+ )
+ .get() as { s: number; m: number }
+ expect(counts).toEqual({ s: 0, m: 0 })
+ } finally {
+ reader.close()
+ }
+ }, 120_000)
+})
diff --git a/mcp-host/src/core/sessionSearch/service.ts b/mcp-host/src/core/sessionSearch/service.ts
index c47c5e673..d21848fa9 100644
--- a/mcp-host/src/core/sessionSearch/service.ts
+++ b/mcp-host/src/core/sessionSearch/service.ts
@@ -91,6 +91,13 @@ export {
sessionSearchRetentionSweep,
}
+/**
+ * Rows one retention-sweep operation may delete. Measured on the real worker
+ * with 400k expired messages: max 123 ms per batch, far below the sync
+ * timeout and the worker progress bound.
+ */
+const RETENTION_SWEEP_BATCH_ROWS = 250
+
export interface SessionSearchServiceDeps {
persistQueue: PersistQueue
}
@@ -137,16 +144,26 @@ export class SessionSearchService {
* whose `ended_at` is older than `retentionDays`. In-flight sessions
* (`end_reason IS NULL`) are always preserved.
*
- * Idempotent. Failures are propagated to the caller — `main.ts` swallows
- * them so a corrupt DB doesn't block Pod startup.
+ * The sweep runs as a sequence of worker operations of at most
+ * `RETENTION_SWEEP_BATCH_ROWS` rows each, so the worker acknowledges every
+ * batch and a large backlog never trips the queue's progress bound. Between
+ * batches an expired session can show part of its history. The cutoff is
+ * fixed before the first batch, so the loop ends once the expired set is
+ * empty.
+ *
+ * Idempotent. Failures are propagated to the caller; `main.ts` logs them.
*/
async sweepRetention(retentionDays: number): Promise<number> {
const cutoffEpoch = Date.now() / 1000 - retentionDays * 86400
- const result = await this.deps.persistQueue.enqueueSync<{ deleted_sessions: number }>({
- kind: 'sweep_closed_sessions',
- cutoffEpoch,
- })
- sessionSearchRetentionSweep?.inc(result.deleted_sessions)
- return result.deleted_sessions
+ let deletedSessions = 0
+ for (;;) {
+ const batch = await this.deps.persistQueue.enqueueSync<{
+ deleted_messages: number
+ deleted_sessions: number
+ }>({ kind: 'sweep_closed_sessions', cutoffEpoch, limit: RETENTION_SWEEP_BATCH_ROWS })
+ sessionSearchRetentionSweep?.inc(batch.deleted_sessions)
+ deletedSessions += batch.deleted_sessions
+ if (batch.deleted_messages === 0 && batch.deleted_sessions === 0) return deletedSessions
+ }
}
}
diff --git a/mcp-host/src/db/statements.ts b/mcp-host/src/db/statements.ts
index c1cd08b90..ce5a96512 100644
--- a/mcp-host/src/db/statements.ts
+++ b/mcp-host/src/db/statements.ts
@@ -52,8 +52,10 @@ export interface PreparedStatements {
ftsSearchByUser: Statement
/** T3.1 — scoped to (userId [, channelType] [, since]) with hard JOIN on `s.id = m.session_id`. */
ftsSearchMessages: Statement
- /** T3.1 — deletes sessions with `end_reason IS NOT NULL` closed before the cutoff. */
- sweepClosedSessions: Statement
+ /** T3.1 — deletes up to `@limit` messages of sessions closed before the cutoff. */
+ sweepClosedSessionMessages: Statement
+ /** T3.1 — deletes up to `@limit` sessions closed before the cutoff. */
+ sweepClosedSessionsEmpty: Statement
integrityCheck: Statement
}
@@ -520,15 +522,31 @@ export function prepareStatements(db: Database): PreparedStatements {
ORDER BY rank
LIMIT @limit
`),
- // T3.1 — retention sweep. Only deletes sessions that are properly closed
+ // T3.1 — retention sweep. Only touches sessions that are properly closed
// (`end_reason IS NOT NULL`) AND aged past the cutoff. In-flight sessions
- // never get pruned, regardless of `started_at`. ON DELETE CASCADE on
- // `messages` cleans up the messages + FTS triggers handle the index.
- sweepClosedSessions: db.prepare(`
+ // never get pruned, regardless of `started_at`. Each statement deletes at
+ // most `@limit` rows so one worker operation stays bounded however large
+ // the backlog is: messages go first (FTS triggers update the index), and
+ // the sessions are deleted once they have no messages left, so their
+ // cascade only reaches `pending_approvals`.
+ sweepClosedSessionMessages: db.prepare(`
+ DELETE FROM messages
+ WHERE id IN (
+ SELECT m.id FROM messages m
+ JOIN sessions s ON s.id = m.session_id
+ WHERE s.end_reason IS NOT NULL
+ AND s.ended_at IS NOT NULL
+ AND s.ended_at < @cutoff
+ LIMIT @limit)
+ `),
+ sweepClosedSessionsEmpty: db.prepare(`
DELETE FROM sessions
- WHERE end_reason IS NOT NULL
- AND ended_at IS NOT NULL
- AND ended_at < @cutoff
+ WHERE id IN (
+ SELECT id FROM sessions
+ WHERE end_reason IS NOT NULL
+ AND ended_at IS NOT NULL
+ AND ended_at < @cutoff
+ LIMIT @limit)
`),
integrityCheck: db.prepare(`PRAGMA integrity_check`),
}
diff --git a/mcp-host/src/db/worker/__tests__/compileSources.ts b/mcp-host/src/db/worker/__tests__/compileSources.ts
new file mode 100644
index 000000000..9bd528814
--- /dev/null
+++ b/mcp-host/src/db/worker/__tests__/compileSources.ts
@@ -0,0 +1,51 @@
+import fs from 'node:fs'
+import path from 'node:path'
+import ts from 'typescript'
+
+/** mcp-host package root. */
+export const MCP_HOST_ROOT = path.resolve(__dirname, '..', '..', '..', '..')
+
+/**
+ * Compiles real mcp-host sources (and everything they import) into `outDir`
+ * with the package tsconfig. Node cannot load `.ts` files with extensionless
+ * imports directly, so tests that need the real worker thread or the real
+ * entrypoint run the compiled output. `outDir`'s parent must link the
+ * package's node_modules so better-sqlite3 and the logger resolve.
+ *
+ * @param roots paths relative to `src/`, e.g. `db/worker/dbWorker.ts`
+ * @returns the compiled `.js` path of each root, in order
+ */
+export function compileSources(roots: readonly string[], outDir: string): string[] {
+ const config = ts.getParsedCommandLineOfConfigFile(
+ path.join(MCP_HOST_ROOT, 'tsconfig.json'),
+ {},
+ {
+ ...ts.sys,
+ onUnRecoverableConfigFileDiagnostic: d => {
+ throw new Error(ts.flattenDiagnosticMessageText(d.messageText, '\n'))
+ },
+ }
+ )
+ if (!config) throw new Error('tsconfig.json could not be parsed')
+ const program = ts.createProgram(
+ roots.map(root => path.join(MCP_HOST_ROOT, 'src', root)),
+ {
+ ...config.options,
+ rootDir: path.join(MCP_HOST_ROOT, 'src'),
+ outDir,
+ noEmit: false,
+ declaration: false,
+ declarationMap: false,
+ sourceMap: false,
+ incremental: false,
+ composite: false,
+ }
+ )
+ const result = program.emit()
+ if (result.emitSkipped) throw new Error(`emit was skipped for ${roots.join(', ')}`)
+ return roots.map(root => {
+ const script = path.join(outDir, root.replace(/\.ts$/, '.js'))
+ if (!fs.existsSync(script)) throw new Error(`compiled output missing: ${script}`)
+ return script
+ })
+}
diff --git a/mcp-host/src/db/worker/__tests__/dbWorker.shutdown.test.ts b/mcp-host/src/db/worker/__tests__/dbWorker.shutdown.test.ts
index edb7ffb00..4bcd01ec2 100644
--- a/mcp-host/src/db/worker/__tests__/dbWorker.shutdown.test.ts
+++ b/mcp-host/src/db/worker/__tests__/dbWorker.shutdown.test.ts
@@ -4,47 +4,12 @@ import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { Worker } from 'node:worker_threads'
-import ts from 'typescript'
import type { SessionRow, WorkerMessage, WorkerReply } from '../protocol'
-
-// The real worker is compiled from source into a temp dir: Node cannot load
-// dbWorker.ts and its extensionless imports directly. The temp dir links the
-// package's node_modules so better-sqlite3 and the logger resolve.
-const MCP_HOST_ROOT = path.resolve(__dirname, '..', '..', '..', '..')
+import { MCP_HOST_ROOT, compileSources } from './compileSources'
let tmpRoot: string
let workerScript: string
-function compileWorker(outDir: string): string {
- const config = ts.getParsedCommandLineOfConfigFile(
- path.join(MCP_HOST_ROOT, 'tsconfig.json'),
- {},
- {
- ...ts.sys,
- onUnRecoverableConfigFileDiagnostic: d => {
- throw new Error(ts.flattenDiagnosticMessageText(d.messageText, '\n'))
- },
- }
- )
- if (!config) throw new Error('tsconfig.json could not be parsed')
- const program = ts.createProgram([path.join(MCP_HOST_ROOT, 'src/db/worker/dbWorker.ts')], {
- ...config.options,
- rootDir: path.join(MCP_HOST_ROOT, 'src'),
- outDir,
- noEmit: false,
- declaration: false,
- declarationMap: false,
- sourceMap: false,
- incremental: false,
- composite: false,
- })
- const result = program.emit()
- if (result.emitSkipped) throw new Error('dbWorker.ts emit was skipped')
- const script = path.join(outDir, 'db', 'worker', 'dbWorker.js')
- if (!fs.existsSync(script)) throw new Error(`compiled worker missing: ${script}`)
- return script
-}
-
function session(id: string): SessionRow {
return {
id,
@@ -96,7 +61,7 @@ function request(worker: Worker, msg: WorkerMessage, onReply?: () => void): Prom
beforeAll(() => {
tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'dbworker-shutdown-'))
fs.symlinkSync(path.join(MCP_HOST_ROOT, 'node_modules'), path.join(tmpRoot, 'node_modules'))
- workerScript = compileWorker(path.join(tmpRoot, 'dist'))
+ ;[workerScript] = compileSources(['db/worker/dbWorker.ts'], path.join(tmpRoot, 'dist'))
}, 60_000)
afterAll(() => {
diff --git a/mcp-host/src/db/worker/dispatcher.ts b/mcp-host/src/db/worker/dispatcher.ts
index 5c54620b4..047bc3445 100644
--- a/mcp-host/src/db/worker/dispatcher.ts
+++ b/mcp-host/src/db/worker/dispatcher.ts
@@ -889,8 +889,11 @@ export async function dispatch(op: WorkerOp, deps: DispatcherDeps): Promise<unkn
case 'sweep_closed_sessions':
return withBusyRetry(() => {
const tx = db.transaction(() => {
- const result = s.sweepClosedSessions.run({ cutoff: op.cutoffEpoch })
- return { deleted_sessions: result.changes }
+ const params = { cutoff: op.cutoffEpoch, limit: op.limit }
+ const deletedMessages = s.sweepClosedSessionMessages.run(params).changes
+ const deletedSessions =
+ deletedMessages === 0 ? s.sweepClosedSessionsEmpty.run(params).changes : 0
+ return { deleted_messages: deletedMessages, deleted_sessions: deletedSessions }
})
return tx.immediate()
})
diff --git a/mcp-host/src/db/worker/protocol.ts b/mcp-host/src/db/worker/protocol.ts
index bd729e9fe..db4d4a618 100644
--- a/mcp-host/src/db/worker/protocol.ts
+++ b/mcp-host/src/db/worker/protocol.ts
@@ -330,7 +330,8 @@ export type WorkerOp =
since?: string
limit: number
}
- | { kind: 'sweep_closed_sessions'; cutoffEpoch: number }
+ /** Deletes at most `limit` rows; the caller repeats until a batch deletes nothing. */
+ | { kind: 'sweep_closed_sessions'; cutoffEpoch: number; limit: number }
| { kind: 'integrity_check' }
| { kind: 'checkpoint' }
| { kind: 'shutdown' }