Skip to content

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

TypeScript

fix(mcp-host): preserve activity across summary rebuilds

Josuéc62f77bd215d
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/modelSelections.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/modelSelections.test.ts
index f7d497983..fbcb46864 100644
--- a/mcp-host/src/core/conversation/persistence/__tests__/modelSelections.test.ts
+++ b/mcp-host/src/core/conversation/persistence/__tests__/modelSelections.test.ts
@@ -110,3 +110,62 @@ describe('R2 — reconstruct.parseModelSelections tolerance', () => {
expect(conversation.modelSelections).toEqual({ openai: 'gpt' })
})
})
+
+describe('reconstructConversation — activity timestamp', () => {
+ it('preserves the materialized activity when retained messages are older', () => {
+ const persistedSession: PersistedSession = {
+ session: {
+ id: 'activity-session',
+ session_key: SESSION_KEY,
+ source: 'rpc',
+ user_id: 'u-1',
+ team_id: null,
+ channel_type: 'rpc',
+ channel_id: 'agent-x',
+ thread_id: 'chat-1',
+ model: null,
+ model_selections: null,
+ system_prompt_stable_hash: null,
+ parent_session_id: null,
+ started_at: 100,
+ last_activity_at: 200,
+ ended_at: null,
+ end_reason: null,
+ message_count: 1,
+ tool_call_count: 0,
+ input_tokens: 0,
+ output_tokens: 0,
+ cache_read_tokens: 0,
+ cache_write_tokens: 0,
+ cache_tokens_reported: 0,
+ title: null,
+ state: 'idle',
+ active_task_id: null,
+ active_trace_context: null,
+ },
+ messages: [
+ {
+ session_id: 'activity-session',
+ ordinal: 0,
+ role: 'user',
+ content: 'retained',
+ content_parts: null,
+ tool_call_id: null,
+ tool_calls: null,
+ tool_name: null,
+ timestamp: 150,
+ token_count: null,
+ finish_reason: null,
+ spillover_ref: null,
+ is_error: 0,
+ turn_number: 1,
+ },
+ ],
+ pending_approval: null,
+ }
+
+ expect(reconstructConversation(persistedSession).conversation.updated_at).toEqual(
+ new Date(200_000)
+ )
+ })
+})
diff --git a/mcp-host/src/core/conversation/persistence/reconstruct.ts b/mcp-host/src/core/conversation/persistence/reconstruct.ts
index 6c0334636..e04e54064 100644
--- a/mcp-host/src/core/conversation/persistence/reconstruct.ts
+++ b/mcp-host/src/core/conversation/persistence/reconstruct.ts
@@ -53,10 +53,14 @@ export function reconstructConversation(persisted: PersistedSession): Reconstruc
const state = mapState(stateString)
const startedAt = new Date(persisted.session.started_at * 1000)
- const lastTimestamp =
- persisted.messages.length > 0
- ? new Date(persisted.messages[persisted.messages.length - 1].timestamp * 1000)
- : startedAt
+ const lastActivityEpoch = persisted.messages.reduce(
+ (latest, message) => Math.max(latest, message.timestamp),
+ Math.max(
+ persisted.session.started_at,
+ persisted.session.last_activity_at ?? persisted.session.started_at
+ )
+ )
+ const lastActivityAt = new Date(lastActivityEpoch * 1000)
const conversation: Conversation = {
id: persisted.session.id,
@@ -67,7 +71,7 @@ export function reconstructConversation(persisted: PersistedSession): Reconstruc
pending_approval: pending,
auto_approved_tools: new Set(),
created_at: startedAt,
- updated_at: lastTimestamp,
+ updated_at: lastActivityAt,
// D.1 — repopulate the in-flight task from the durable column. After a pod
// restart this may point at a task whose reporter is gone (ghost); the D.2
// processing reaper reconciles that at boot.
diff --git a/mcp-host/src/db/migrations/010-materialized-session-summaries.ts b/mcp-host/src/db/migrations/010-materialized-session-summaries.ts
index f73ac0830..9ba1df795 100644
--- a/mcp-host/src/db/migrations/010-materialized-session-summaries.ts
+++ b/mcp-host/src/db/migrations/010-materialized-session-summaries.ts
@@ -16,9 +16,12 @@ export function up(db: Database): void {
ALTER TABLE sessions ADD COLUMN turn_count INTEGER NOT NULL DEFAULT 0;
UPDATE sessions
- SET last_activity_at = COALESCE(
- (SELECT MAX(m.timestamp) FROM messages m WHERE m.session_id = sessions.id),
- started_at
+ SET last_activity_at = MAX(
+ started_at,
+ COALESCE(
+ (SELECT MAX(m.timestamp) FROM messages m WHERE m.session_id = sessions.id),
+ started_at
+ )
),
turn_count = (
SELECT COUNT(DISTINCT m.turn_number)
diff --git a/mcp-host/src/db/migrations/__tests__/010-materialized-session-summaries.test.ts b/mcp-host/src/db/migrations/__tests__/010-materialized-session-summaries.test.ts
index 1a66f4e9a..4293fd33f 100644
--- a/mcp-host/src/db/migrations/__tests__/010-materialized-session-summaries.test.ts
+++ b/mcp-host/src/db/migrations/__tests__/010-materialized-session-summaries.test.ts
@@ -17,6 +17,7 @@ describe('migration 010 — materialized session summaries', () => {
)
insertSession.run('session-with-messages', 'u-1:rpc:agent:chat-1', 1)
insertSession.run('empty-session', 'u-1:rpc:agent:chat-2', 5)
+ insertSession.run('clock-skewed-session', 'u-1:rpc:agent:chat-3', 100)
const insertMessage = db.prepare(
`INSERT INTO messages (
@@ -28,6 +29,7 @@ describe('migration 010 — materialized session summaries', () => {
insertMessage.run('session-with-messages', 2, 'tool', 'result', null, 11, 1)
insertMessage.run('session-with-messages', 3, 'assistant', 'answer', null, 12, 1)
insertMessage.run('session-with-messages', 4, 'user', 'next', null, 20, 2)
+ insertMessage.run('clock-skewed-session', 0, 'user', 'early', null, 20, 1)
m010.up(db)
@@ -57,6 +59,11 @@ describe('migration 010 — materialized session summaries', () => {
turn_count: 0,
message_count: 0,
})
+ expect(
+ db
+ .prepare(`SELECT last_activity_at FROM sessions WHERE id = 'clock-skewed-session'`)
+ .get()
+ ).toEqual({ last_activity_at: 100 })
const indexes = db
.prepare(
diff --git a/mcp-host/src/db/statements.ts b/mcp-host/src/db/statements.ts
index 96c160516..ceb3e7c2e 100644
--- a/mcp-host/src/db/statements.ts
+++ b/mcp-host/src/db/statements.ts
@@ -209,6 +209,7 @@ export function prepareStatements(db: Database): PreparedStatements {
recomputeSessionMessageSummary: db.prepare(`
UPDATE sessions
SET last_activity_at = MAX(
+ COALESCE(last_activity_at, started_at),
started_at,
COALESCE(
(SELECT MAX(timestamp) FROM messages WHERE session_id = @id),
diff --git a/mcp-host/src/db/worker/__tests__/dispatcher.test.ts b/mcp-host/src/db/worker/__tests__/dispatcher.test.ts
index 704f4ba35..99b82631d 100644
--- a/mcp-host/src/db/worker/__tests__/dispatcher.test.ts
+++ b/mcp-host/src/db/worker/__tests__/dispatcher.test.ts
@@ -310,6 +310,46 @@ describe('dbWorker dispatcher', () => {
expect(readSummary()).toEqual(incrementalSummary)
})
+ it('does not move session activity backward when replacing retained messages', async () => {
+ const deps = createDispatcher(db)
+ const session = {
+ ...makeSession('conv-replace-activity', 'u-replace:rpc:agent:default'),
+ started_at: 100,
+ }
+ await dispatch({ kind: 'insert_session', payload: session }, deps)
+ db.prepare('UPDATE sessions SET last_activity_at = ? WHERE id = ?').run(200, session.id)
+
+ await dispatch(
+ {
+ kind: 'replace_messages',
+ sessionId: session.id,
+ messages: [
+ {
+ session_id: session.id,
+ ordinal: 0,
+ role: 'user',
+ content: 'retained summary',
+ content_parts: null,
+ tool_call_id: null,
+ tool_calls: null,
+ tool_name: null,
+ timestamp: 150,
+ token_count: null,
+ finish_reason: null,
+ spillover_ref: null,
+ is_error: 0,
+ turn_number: 1,
+ },
+ ],
+ },
+ deps
+ )
+
+ expect(
+ db.prepare('SELECT last_activity_at FROM sessions WHERE id = ?').get(session.id)
+ ).toEqual({ last_activity_at: 200 })
+ })
+
it('separates tool-call storage rows from visible messages and token counters', async () => {
const deps = createDispatcher(db)
await dispatch(