Skip to content

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

TypeScript

fix(mcp-host): await the boot retention sweep before bootstrap

Alfredo Lopez1ab582810757
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index ea6dc25e1..159bd355e 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.587",
+ "version": "0.1.588",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.587",
+ "version": "0.1.588",
"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 f9cc888a8..d4dd49eef 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.587",
+ "version": "0.1.588",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjs b/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjs
index faf7583c8..5f30c8e77 100644
--- a/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjs
+++ b/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjs
@@ -7,6 +7,9 @@
// as if the worker were wedged in its close checkpoint.
// - DB_WORKER_CONTROL=terminate-on-sigusr2: SIGUSR2 terminates the DB worker,
// an unexpected worker exit while the Host is serving.
+// - DB_WORKER_CONTROL=record-ops: every message reaches the worker; the
+// preload prints `PRELOAD_POST <kind>` for each op posted and
+// `PRELOAD_REPLY <kind>` for each reply, in the order they happen.
//
// Every other message reaches the real worker unchanged.
//
@@ -24,7 +27,7 @@ sigtermReady.unref()
const RealWorker = workerThreads.Worker
const mode = process.env.DB_WORKER_CONTROL
-if (mode !== 'withhold-shutdown' && mode !== 'terminate-on-sigusr2') {
+if (mode !== 'withhold-shutdown' && mode !== 'terminate-on-sigusr2' && mode !== 'record-ops') {
throw new Error(`dbWorkerControl: unknown DB_WORKER_CONTROL '${mode}'`)
}
@@ -38,6 +41,17 @@ class ControlledWorker extends RealWorker {
void this.terminate()
})
}
+ if (this.isDbWorker && mode === 'record-ops') {
+ this.postedKinds = new Map()
+ // Registered before the PersistQueue's listener, so a reply is printed
+ // before the queue resolves its caller.
+ this.on('message', reply => {
+ const kind = this.postedKinds.get(reply?.id)
+ if (kind === undefined) return
+ this.postedKinds.delete(reply.id)
+ process.stdout.write(`PRELOAD_REPLY ${kind}\n`)
+ })
+ }
}
postMessage(msg, transfer) {
@@ -45,6 +59,10 @@ class ControlledWorker extends RealWorker {
process.stdout.write('PRELOAD_WITHHELD_SHUTDOWN\n')
return
}
+ if (this.isDbWorker && mode === 'record-ops' && msg?.op?.kind) {
+ this.postedKinds.set(msg.id, msg.op.kind)
+ process.stdout.write(`PRELOAD_POST ${msg.op.kind}\n`)
+ }
return super.postMessage(msg, transfer)
}
}
diff --git a/mcp-host/src/__tests__/main.persistFailureExit.test.ts b/mcp-host/src/__tests__/main.persistFailureExit.test.ts
index 49f411874..ed51ced0c 100644
--- a/mcp-host/src/__tests__/main.persistFailureExit.test.ts
+++ b/mcp-host/src/__tests__/main.persistFailureExit.test.ts
@@ -1,8 +1,10 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
+import Database from 'better-sqlite3'
import { spawn } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
+import { runMigrations } from '../db/migrate'
import { MCP_HOST_ROOT, compileSources } from '../db/worker/__tests__/compileSources'
// Drives the real compiled main.js (dev mode, SQLite store, real DB worker
@@ -25,11 +27,13 @@ interface ChildRun {
}
function runHost(
- control: 'withhold-shutdown' | 'terminate-on-sigusr2',
+ control: 'withhold-shutdown' | 'terminate-on-sigusr2' | 'record-ops',
signal: 'SIGTERM' | 'SIGUSR2',
- readyMarker: string
+ readyMarker: string,
+ extra: { env?: Record<string, string>; seed?: (dbPath: string) => void } = {}
): Promise<ChildRun> {
const home = fs.mkdtempSync(path.join(tmpRoot, 'host-'))
+ extra.seed?.(path.join(home, 'state.db'))
const child = spawn(process.execPath, ['--require', PRELOAD, mainScript], {
cwd: home,
env: {
@@ -46,6 +50,7 @@ function runHost(
CLERUM_DB_PERSIST_SYNC_TIMEOUT_MS: '500',
CLERUM_DB_WORKER_STALL_TIMEOUT_MS: String(STALL_TIMEOUT_MS),
DB_WORKER_CONTROL: control,
+ ...extra.env,
},
})
return new Promise(resolve => {
@@ -119,4 +124,49 @@ describe('main.ts persist-failure exit wiring (real entrypoint)', () => {
)
expect({ code: run.code, signal: run.signal }).toEqual({ code: 1, signal: null })
}, 30_000)
+
+ it('finishes the boot retention sweep before bootstrap reads the database', async () => {
+ // One chat whose approval was cancelled 100 days ago, with one message:
+ // the values the only producer writes (approval cancel). The sweep needs
+ // a batch for the message, one for the session and an empty one.
+ const seed = (dbPath: string) => {
+ const db = new Database(dbPath)
+ try {
+ runMigrations(db)
+ const endedAt = Date.now() / 1000 - 100 * 86400
+ db.prepare(
+ `INSERT INTO sessions (id, session_key, source, started_at, ended_at, end_reason)
+ VALUES ('s-expired', 'u-1:rpc:agent:chat-1', 'rpc', ?, ?, 'cancelled')`
+ ).run(endedAt - 60, endedAt)
+ db.prepare(
+ `INSERT INTO messages (session_id, ordinal, role, content, timestamp)
+ VALUES ('s-expired', 0, 'user', 'hello', ?)`
+ ).run(endedAt - 30)
+ } finally {
+ db.close()
+ }
+ }
+ const run = await runHost('record-ops', 'SIGTERM', 'PRELOAD_SIGTERM_HANDLER_READY', {
+ env: { CLERUM_SESSION_SEARCH_ENABLED: 'true' },
+ seed,
+ })
+
+ const events = run.output
+ .split('\n')
+ .filter(line => line.startsWith('PRELOAD_POST ') || line.startsWith('PRELOAD_REPLY '))
+ // Witnesses: the sweep ran in more than one batch, bootstrap posted its
+ // first DB op, and the sweep deleted the seeded session.
+ expect(
+ events.filter(e => e === 'PRELOAD_POST sweep_closed_sessions').length,
+ run.output
+ ).toBeGreaterThanOrEqual(2)
+ const firstBootstrapOp = events.indexOf('PRELOAD_POST reap_processing_sessions')
+ expect(firstBootstrapOp, run.output).toBeGreaterThan(-1)
+ expect(run.output).toMatch(/"event":"search_retention_sweep"[^\n]*"deleted_sessions":1\b/)
+ // The last sweep reply comes before bootstrap posts anything.
+ const lastSweepReply = events.lastIndexOf('PRELOAD_REPLY sweep_closed_sessions')
+ expect(lastSweepReply, run.output).toBeGreaterThan(-1)
+ expect(lastSweepReply, events.join('\n')).toBeLessThan(firstBootstrapOp)
+ expect({ code: run.code, signal: run.signal }).toEqual({ code: 0, signal: null })
+ }, 30_000)
})
diff --git a/mcp-host/src/core/sessionSearch/service.ts b/mcp-host/src/core/sessionSearch/service.ts
index d21848fa9..34af5b417 100644
--- a/mcp-host/src/core/sessionSearch/service.ts
+++ b/mcp-host/src/core/sessionSearch/service.ts
@@ -146,10 +146,12 @@ export class SessionSearchService {
*
* 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.
+ * batch and a large backlog never trips the queue's progress bound. A batch
+ * deletes an expired session's messages before its row, so between batches
+ * the session would load with part of its history; `main.ts` awaits the
+ * whole sweep before bootstrap and before the Host serves traffic, so no
+ * load runs between batches. 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.
*/
diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.ts
index 3c11580ae..10bbf93aa 100644
--- a/mcp-host/src/main.ts
+++ b/mcp-host/src/main.ts
@@ -1896,21 +1896,22 @@ async function initializeAgent(): Promise<void> {
// Boot-only retention sweep (T3.1 §8). Failures are non-fatal so a
// corrupt DB does not block traffic — the metric / log surfaces the issue.
- sessionSearchService.sweepRetention(config.searchRetentionDays).then(
- deleted => {
- logger.info(
- {
- event: 'search_retention_sweep',
- deleted_sessions: deleted,
- retention_days: config.searchRetentionDays,
- },
- 'Host runtime event'
- )
- },
- err => {
- logger.error({ err: err }, '[Main] Session search retention sweep failed (non-fatal):')
- }
- )
+ // Awaited before bootstrap: the sweep runs in batches that delete an
+ // expired session's messages before the session row, so no load may run
+ // between them. It therefore counts against the Host startup window.
+ try {
+ const deleted = await sessionSearchService.sweepRetention(config.searchRetentionDays)
+ logger.info(
+ {
+ event: 'search_retention_sweep',
+ deleted_sessions: deleted,
+ retention_days: config.searchRetentionDays,
+ },
+ 'Host runtime event'
+ )
+ } catch (err) {
+ logger.error({ err: err }, '[Main] Session search retention sweep failed (non-fatal):')
+ }
logger.info(
{ searchRetentionDays: config.searchRetentionDays },
'[Main] Session search enabled (retentionDays=)'