Skip to content

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

TypeScript

fix(mcp-host): index sessions.parent_session_id for the retention sweep

Alfredo Lopez697c8a128a8b
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 3c27c019e..6d0e027ac 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.585",
+ "version": "0.1.586",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.585",
+ "version": "0.1.586",
"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 934da2127..b89b95db0 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.585",
+ "version": "0.1.586",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/db/migrations/017-session-parent-index.ts b/mcp-host/src/db/migrations/017-session-parent-index.ts
new file mode 100644
index 000000000..58532e480
--- /dev/null
+++ b/mcp-host/src/db/migrations/017-session-parent-index.ts
@@ -0,0 +1,22 @@
+/**
+ * Migration 017 — index the self-referencing `sessions.parent_session_id`.
+ *
+ * With `foreign_keys = ON`, deleting a session row checks that no other
+ * session names it as its parent. Without an index on the child key that
+ * check scans the whole table for every deleted row, so one retention-sweep
+ * batch grows with the size of `sessions` instead of with its `limit`.
+ */
+import type { Database } from 'better-sqlite3'
+
+export const name = '017-session-parent-index'
+
+export function up(db: Database): void {
+ db.exec(`
+ CREATE INDEX IF NOT EXISTS idx_sessions_parent_session_id
+ ON sessions(parent_session_id);
+ `)
+}
+
+export function down(db: Database): void {
+ db.exec(`DROP INDEX IF EXISTS idx_sessions_parent_session_id;`)
+}
diff --git a/mcp-host/src/db/migrations/__tests__/008-active-trace-context.test.ts b/mcp-host/src/db/migrations/__tests__/008-active-trace-context.test.ts
index 55e1751c0..074ad712d 100644
--- a/mcp-host/src/db/migrations/__tests__/008-active-trace-context.test.ts
+++ b/mcp-host/src/db/migrations/__tests__/008-active-trace-context.test.ts
@@ -78,6 +78,7 @@ describe('migration 008 — active trace context', () => {
'014-pending-approval-task-budget',
'015-session-model-selection-revision',
'016-pending-approval-authorization-scope',
+ '017-session-parent-index',
],
pending: [],
})
diff --git a/mcp-host/src/db/migrations/__tests__/017-session-parent-index.test.ts b/mcp-host/src/db/migrations/__tests__/017-session-parent-index.test.ts
new file mode 100644
index 000000000..f636b1b01
--- /dev/null
+++ b/mcp-host/src/db/migrations/__tests__/017-session-parent-index.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, it } from 'vitest'
+import Database from 'better-sqlite3'
+import { migrations } from '../index'
+
+const NAME = '017-session-parent-index'
+
+function indexedColumns(db: Database.Database, table: string): string[][] {
+ const indexes = db.prepare(`PRAGMA index_list(${table})`).all() as Array<{ name: string }>
+ return indexes.map(index =>
+ (db.prepare(`PRAGMA index_info(${index.name})`).all() as Array<{ name: string }>).map(
+ column => column.name
+ )
+ )
+}
+
+describe('migration 017 — index on sessions(parent_session_id)', () => {
+ it('is registered last and indexes the self-referencing parent key', () => {
+ const names = migrations.map(m => m.name)
+ expect(names.at(-1)).toBe(NAME)
+ const migration = migrations.find(m => m.name === NAME)!
+
+ const db = new Database(':memory:')
+ try {
+ for (const m of migrations) {
+ if (m.name === NAME) break
+ m.up(db)
+ }
+ db.prepare(
+ `INSERT INTO sessions (id, session_key, source, started_at) VALUES ('s-1', 'u:rpc:a:1', 'rpc', 0)`
+ ).run()
+ expect(indexedColumns(db, 'sessions')).not.toContainEqual(['parent_session_id'])
+
+ migration.up(db)
+ expect(indexedColumns(db, 'sessions')).toContainEqual(['parent_session_id'])
+ // Liveness witness: the index was built over an existing row, which survives.
+ expect(db.prepare('SELECT id, parent_session_id FROM sessions').all()).toEqual([
+ { id: 's-1', parent_session_id: null },
+ ])
+
+ migration.down(db)
+ expect(indexedColumns(db, 'sessions')).not.toContainEqual(['parent_session_id'])
+ } finally {
+ db.close()
+ }
+ })
+})
diff --git a/mcp-host/src/db/migrations/index.ts b/mcp-host/src/db/migrations/index.ts
index 19d4febd8..93de2abaa 100644
--- a/mcp-host/src/db/migrations/index.ts
+++ b/mcp-host/src/db/migrations/index.ts
@@ -14,6 +14,7 @@ import * as pendingApprovalConnect from './013-pending-approval-connect'
import * as pendingApprovalTaskBudget from './014-pending-approval-task-budget'
import * as sessionModelSelectionRevision from './015-session-model-selection-revision'
import * as pendingApprovalAuthorizationScope from './016-pending-approval-authorization-scope'
+import * as sessionParentIndex from './017-session-parent-index'
/**
* Ordered list of migrations. New migrations append; never reorder or rename.
@@ -41,4 +42,5 @@ export const migrations: Array<{
pendingApprovalTaskBudget,
sessionModelSelectionRevision,
pendingApprovalAuthorizationScope,
+ sessionParentIndex,
]