Skip to content

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

TypeScript

fix: preserve live chat turns and align session counts

Josué538c85bb5f39
diff --git a/desktop-app/package-lock.json b/desktop-app/package-lock.json
index 0100996b1..2c8edbfe5 100644
--- a/desktop-app/package-lock.json
+++ b/desktop-app/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "evenfire",
- "version": "0.1.342",
+ "version": "0.1.343",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "evenfire",
- "version": "0.1.342",
+ "version": "0.1.343",
"license": "MPL-2.0",
"dependencies": {
"@tanstack/react-query": "5.100.10",
diff --git a/desktop-app/package.json b/desktop-app/package.json
index 901c33b0c..31b80d921 100644
--- a/desktop-app/package.json
+++ b/desktop-app/package.json
@@ -1,7 +1,7 @@
{
"name": "evenfire",
"productName": "Evenfire",
- "version": "0.1.342",
+ "version": "0.1.343",
"private": true,
"license": "MPL-2.0",
"description": "Evenfire desktop client for secure agent and workflow access",
diff --git a/desktop-app/src/__tests__/chatMessageMerge.test.ts b/desktop-app/src/__tests__/chatMessageMerge.test.ts
index 87e17583d..f5985b4f4 100644
--- a/desktop-app/src/__tests__/chatMessageMerge.test.ts
+++ b/desktop-app/src/__tests__/chatMessageMerge.test.ts
@@ -75,7 +75,7 @@ describe('mergeAuthoritativeServerMessages', () => {
expect(merged.map(message => message.id)).toEqual(['turn-3-user', 'pending'])
})
- it('does not duplicate an active optimistic message when the server returns its turn', () => {
+ it('keeps an active optimistic message until the server identifies its task', () => {
const serverMessages = turnsToChatMessages([
{
number: 2,
@@ -106,6 +106,39 @@ describe('mergeAuthoritativeServerMessages', () => {
expect(merged).toEqual([
expect.objectContaining({ id: 'turn-1-user' }),
+ expect.objectContaining({
+ id: 'optimistic-task-2',
+ content: 'second',
+ task_id: 'task-2',
+ }),
+ ])
+ })
+
+ it('replaces an active optimistic message when the server identifies the same task', () => {
+ const merged = mergeAuthoritativeServerMessages(
+ [
+ {
+ id: 'optimistic-task-2',
+ role: 'user',
+ content: 'second',
+ timestamp: 2,
+ task_id: 'task-2',
+ },
+ ],
+ [
+ {
+ id: 'turn-2-user',
+ role: 'user',
+ content: 'second',
+ timestamp: 3,
+ task_id: 'task-2',
+ serverTurnNumber: 2,
+ },
+ ],
+ { activeTaskIds: new Set(['task-2']) }
+ )
+
+ expect(merged).toEqual([
expect.objectContaining({
id: 'turn-2-user',
content: 'second',
@@ -608,6 +641,7 @@ describe('mergeAuthoritativeServerMessages', () => {
)
const turnFive = merged.find(message => message.id === 'turn-5-user')
+ expect(merged.map(message => message.id)).toContain('live-optimistic')
expect(turnFive?.task_id).toBeUndefined()
expect(turnFive?.attachments).toBeUndefined()
})
diff --git a/desktop-app/src/chatMessageMerge.ts b/desktop-app/src/chatMessageMerge.ts
index d556bb02d..b513ee2b7 100644
--- a/desktop-app/src/chatMessageMerge.ts
+++ b/desktop-app/src/chatMessageMerge.ts
@@ -124,6 +124,7 @@ export function mergeAuthoritativeServerMessages(
const removedIndexByMessage = new Map<ChatMessage, number>()
const localByServerMessage = new Map<ChatMessage, ChatMessage>()
const consumedLocalMessages = new Set<ChatMessage>()
+ const suppressedServerMessages = new Set<ChatMessage>()
const markLocalReplacement = (serverMessage: ChatMessage, localMessage: ChatMessage) => {
const index = existing.indexOf(localMessage)
@@ -180,19 +181,40 @@ export function mergeAuthoritativeServerMessages(
}
if (message.role !== 'user' && message.role !== 'assistant') continue
if (message.isError || message.preserveLocal || consumedLocalMessages.has(message)) continue
- const matchingServerMessage = authoritative.find(
+ const eligibleServerMessages = authoritative.filter(
serverMessage =>
!localByServerMessage.has(serverMessage) &&
+ !suppressedServerMessages.has(serverMessage) &&
serverMessage.role === message.role &&
serverTurnAllowedForTurnlessLocal(serverMessage, message, index)
)
- if (matchingServerMessage) markLocalReplacement(matchingServerMessage, message)
+ // A live local bubble remains authoritative for the UI until the server can
+ // identify the same task. Current server history rows are usually unscoped,
+ // so suppress one only when its role and position identify a single slot.
+ const exactTaskMatches = eligibleServerMessages.filter(
+ serverMessage => serverMessage.task_id === message.task_id
+ )
+ if (exactTaskMatches.length === 1) {
+ markLocalReplacement(exactTaskMatches[0]!, message)
+ continue
+ }
+
+ const unscopedCandidates = eligibleServerMessages.filter(
+ serverMessage => serverMessage.task_id === undefined
+ )
+ const candidateTurns = new Set(
+ unscopedCandidates.map(serverMessage => messageServerTurnNumber(serverMessage)!)
+ )
+ if (candidateTurns.size === 1 && unscopedCandidates.length === 1) {
+ suppressedServerMessages.add(unscopedCandidates[0]!)
+ }
}
for (const [index, message] of existing.entries()) {
if (messageServerTurnNumber(message) !== undefined) continue
if (message.role !== 'user' && message.role !== 'assistant') continue
if (message.isError || message.preserveLocal || consumedLocalMessages.has(message)) continue
+ if (message.task_id && options.activeTaskIds?.has(message.task_id)) continue
const authoritativeCandidates = authoritative.filter(
serverMessage =>
@@ -214,13 +236,15 @@ export function mergeAuthoritativeServerMessages(
markLocalReplacement(candidates[0]!, message)
}
- const hydratedReplacements = authoritative.map(message => {
- const local = localByServerMessage.get(message)
- return {
- message: preferredServerMessage(message, local, { copyLocalMetadata: Boolean(local) }),
- localAnchorIndex: local ? removedIndexByMessage.get(local) : undefined,
- }
- })
+ const hydratedReplacements = authoritative
+ .filter(message => !suppressedServerMessages.has(message))
+ .map(message => {
+ const local = localByServerMessage.get(message)
+ return {
+ message: preferredServerMessage(message, local, { copyLocalMetadata: Boolean(local) }),
+ localAnchorIndex: local ? removedIndexByMessage.get(local) : undefined,
+ }
+ })
const replacementAnchorByTurn = new Map<number, number>()
for (const replacement of hydratedReplacements) {
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 74878c544..fcc5bc243 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.222",
+ "version": "0.1.223",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.222",
+ "version": "0.1.223",
"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 61ac20f36..eb8a4981a 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.222",
+ "version": "0.1.223",
"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/statements.ts b/mcp-host/src/db/statements.ts
index 526daeeac..e2ec7e263 100644
--- a/mcp-host/src/db/statements.ts
+++ b/mcp-host/src/db/statements.ts
@@ -193,11 +193,12 @@ export function prepareStatements(db: Database): PreparedStatements {
ELSE last_activity_at
END,
turn_count = turn_count + CASE
- WHEN (
+ WHEN @turn_number IS NOT NULL
+ AND (
SELECT COUNT(*)
FROM messages
WHERE session_id = @id
- AND COALESCE(turn_number, 0) = COALESCE(@turn_number, 0)
+ AND turn_number = @turn_number
) = 1
THEN 1
ELSE 0
diff --git a/mcp-host/src/db/worker/__tests__/dispatcher.test.ts b/mcp-host/src/db/worker/__tests__/dispatcher.test.ts
index b1b943ce2..9114652ef 100644
--- a/mcp-host/src/db/worker/__tests__/dispatcher.test.ts
+++ b/mcp-host/src/db/worker/__tests__/dispatcher.test.ts
@@ -223,7 +223,7 @@ describe('dbWorker dispatcher', () => {
const incrementalSummary = readSummary()
expect(incrementalSummary).toEqual({
last_activity_at: 20,
- turn_count: 3,
+ turn_count: 2,
message_count: 3,
})