Skip to content

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

TypeScript

fix(desktop): R1-M3 persist authoritative message updates

Josuéd63c05a8dafb
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx
index e2e121f4e..b9e0ba623 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx
@@ -76,6 +76,49 @@ describe('switchToChat (unified, D.4)', () => {
expect(result.current.chatMessages).toHaveLength(2)
})
+ it('persists authoritative content changes when stable message IDs are unchanged', async () => {
+ clerum.chat.loadMessages.mockResolvedValue([
+ {
+ id: 'turn-1-user',
+ role: 'user',
+ content: 'old question',
+ timestamp: Date.parse('2026-05-28T10:01:00Z'),
+ serverTurnNumber: 1,
+ },
+ {
+ id: 'turn-1-assistant',
+ role: 'assistant',
+ content: 'old answer',
+ timestamp: Date.parse('2026-05-28T10:01:05Z'),
+ serverTurnNumber: 1,
+ },
+ ])
+ clerum.rpc.loadSessionMessages.mockResolvedValue({
+ agent: 'agent-x',
+ chatId: 'content-update',
+ state: 'idle',
+ turns: [turn(1, 'new question', 'new answer')],
+ })
+ const { result } = renderController()
+ await settleMount()
+
+ await act(async () => {
+ await result.current.switchToChat('agent-x', 'content-update')
+ })
+
+ expect(result.current.chatMessages.map(message => message.content)).toEqual([
+ 'new question',
+ 'new answer',
+ ])
+ expect(clerum.chat.replaceMessages).toHaveBeenCalledWith(
+ 'agent-x',
+ 'content-update',
+ expect.arrayContaining([
+ expect.objectContaining({ id: 'turn-1-assistant', content: 'new answer' }),
+ ])
+ )
+ })
+
it('does not overwrite a long chat whose cache already matches the server (no 50-cap false-stale)', async () => {
// 30 turns → 60 messages, well past any windowing limit.
const turns = Array.from({ length: 30 }, (_, i) => turn(i + 1, `q${i + 1}`, `a${i + 1}`))
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 39cc4baa9..e871dc8d7 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -213,15 +213,29 @@ function sameMessageSequence(a: AgentChatMessage[], b: AgentChatMessage[]): bool
a.length === b.length &&
a.every((message, index) => {
const other = b[index]
- return (
- other !== undefined &&
- message.id === other.id &&
- serverTurnNumber(message) === serverTurnNumber(other)
- )
+ if (!other) return false
+ const { timestamp: _timestamp, serverTurnNumber: _serverTurnNumber, ...comparable } = message
+ const {
+ timestamp: _otherTimestamp,
+ serverTurnNumber: _otherServerTurnNumber,
+ ...otherComparable
+ } = other
+ return stableJson(comparable) === stableJson(otherComparable)
})
)
}
+function stableJson(value: unknown): string {
+ return JSON.stringify(value, (_key, nested: unknown) => {
+ if (nested === null || typeof nested !== 'object' || Array.isArray(nested)) return nested
+ return Object.fromEntries(
+ Object.entries(nested as Record<string, unknown>).sort(([left], [right]) =>
+ left.localeCompare(right)
+ )
+ )
+ })
+}
+
/** Keep only the last `n` insertion-ordered keys of a per-message map. Returns the
* same reference when already within the cap (so React state stays identity-stable). */
function pruneToLastN<T>(map: Record<string, T>, n: number): Record<string, T> {