Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): gate idle echo collapse on authoritative row (R2-H1)
Diego Zacariasfc4a097b2d08
diff --git a/desktop-app/package-lock.json b/desktop-app/package-lock.jsonindex cd6097964..22cb99b82 100644--- a/desktop-app/package-lock.json+++ b/desktop-app/package-lock.json@@ -1,12 +1,12 @@{"name": "evenfire",- "version": "0.1.346",+ "version": "0.1.347","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "evenfire",- "version": "0.1.346",+ "version": "0.1.347","license": "MPL-2.0","dependencies": {"@tanstack/react-query": "5.100.10",diff --git a/desktop-app/package.json b/desktop-app/package.jsonindex 2710bc5ba..1ad6e8bdb 100644--- a/desktop-app/package.json+++ b/desktop-app/package.json@@ -1,7 +1,7 @@{"name": "evenfire","productName": "Evenfire",- "version": "0.1.346",+ "version": "0.1.347","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.tsindex cf324c605..d1e02b25d 100644--- a/desktop-app/src/__tests__/chatMessageMerge.test.ts+++ b/desktop-app/src/__tests__/chatMessageMerge.test.ts@@ -1304,6 +1304,49 @@ describe('mergeAuthoritativeServerMessages — R2-H1 idle echo liveness (§6.1,expect(reMerge.map(m => m.id)).toEqual(['turn-1-user', 'turn-2-user'])})+ it('compares the idle echo against the authoritative incoming row, not the stale disk neighbour (STALE/FRESH)', () => {+ // Regression for the R2-H1 content-gate defect: the collapse gate must compare+ // the idle local against the AUTHORITATIVE incoming row that fills the neighbour+ // slot, not against the stale numbered neighbour from `existing`. Here the disk+ // turn-2-user still holds the pre-reconcile text 'STALE'; incoming carries the+ // fresh 'FRESH' for that same slot. The idle optimistic content 'STALE' equals+ // the STALE disk neighbour but NOT the FRESH authoritative row that actually+ // lands in the output. Dropping it would erase local text that is nowhere in the+ // output. Server rows are derived from the real producer (T1).+ const incoming = turnsToChatMessages([+ { number: 1, user_input: 'A', started_at: new Date(1).toISOString() },+ { number: 2, user_input: 'FRESH', started_at: new Date(3).toISOString() },+ ])+ const existing: ChatMessage[] = [+ { id: 'turn-1-user', role: 'user', content: 'A', timestamp: 1, serverTurnNumber: 1 },+ {+ id: 'optimistic-stale',+ role: 'user',+ content: 'STALE',+ timestamp: 2,+ task_id: 'done-task',+ },+ { id: 'turn-2-user', role: 'user', content: 'STALE', timestamp: 2, serverTurnNumber: 2 },+ ]++ const merged = mergeAuthoritativeServerMessages(existing, incoming, {+ activeTaskIds: new Set(),+ })+ const ids = merged.map(m => m.id)++ // The local's text must survive: the optimistic is not dropped, because its+ // content matches neither authoritative neighbour (turn-1='A', turn-2='FRESH').+ expect(ids).toContain('optimistic-stale')+ expect(merged.find(m => m.id === 'optimistic-stale')).toEqual(+ expect.objectContaining({ content: 'STALE' })+ )+ // 'STALE' text is present in the output at least once (via the surviving local).+ expect(merged.some(m => m.content === 'STALE')).toBe(true)+ // Non-reintroduction of R1-B1: both authoritative rows survive, carrying FRESH.+ expect(ids).toEqual(expect.arrayContaining(['turn-1-user', 'turn-2-user']))+ expect(merged.find(m => m.id === 'turn-2-user')?.content).toBe('FRESH')+ })+it('does NOT drop an idle orphan whose content diverges from its numbered neighbours (gate (b))', () => {// The key test of remediation (b): an accepted task that never registered a// server turn (cancelled-in-queue / budget-denied / persistTurnStart failure,@@ -1363,14 +1406,21 @@ describe('mergeAuthoritativeServerMessages — R2-H1 idle echo liveness (§6.1,const scenarioArb = fc.record({start: fc.integer({ min: 1, max: 6 }),role: fc.constantFrom<'user' | 'assistant'>('user', 'assistant'),- // true -> echo (content mirrors the higher numbered neighbour), false -> orphan.- isEcho: fc.boolean(),+ // fresh-echo -> content mirrors the AUTHORITATIVE incoming row (collapses when+ // idle); stale-echo -> content mirrors a STALE disk neighbour that diverges+ // from incoming for the same slot (must NOT collapse — its text is nowhere+ // in the output otherwise); orphan -> content matches neither (survives).+ bubbleMode: fc.constantFrom<'fresh-echo' | 'stale-echo' | 'orphan'>(+ 'fresh-echo',+ 'stale-echo',+ 'orphan'+ ),// If active, the bubble is optimistic-live and must never collapse (I-5).active: fc.boolean(),})fc.assert(- fc.property(scenarioArb, ({ start, role, isEcho, active }) => {+ fc.property(scenarioArb, ({ start, role, bubbleMode, active }) => {const turns = [{ number: start, user_input: `q${start}`, started_at: new Date(start).toISOString() },{@@ -1386,7 +1436,21 @@ describe('mergeAuthoritativeServerMessages — R2-H1 idle echo liveness (§6.1,const numbered = turnsToChatMessages(turns).filter(m => m.role === role)if (numbered.length !== 2) returnconst [low, high] = numbered- const bubbleContent = isEcho ? high!.content : `orphan-${start}`+ const incoming = numbered+ // The disk copy of the higher numbered neighbour. In stale-echo it carries a+ // pre-reconcile text that diverges from the authoritative incoming row for the+ // same (turn, role) slot; the first loop replaces it with the fresh incoming+ // row in the output, so a gate comparing against this disk row would false-+ // match and drop a local whose text never reaches the output.+ const staleContent = `stale-${start + 1}`+ const diskHigh: ChatMessage =+ bubbleMode === 'stale-echo' ? { ...high!, content: staleContent } : high!+ const bubbleContent =+ bubbleMode === 'fresh-echo'+ ? high!.content+ : bubbleMode === 'stale-echo'+ ? staleContent+ : `orphan-${start}`const bubble: ChatMessage = {id: 'sandwiched-bubble',role,@@ -1394,20 +1458,27 @@ describe('mergeAuthoritativeServerMessages — R2-H1 idle echo liveness (§6.1,timestamp: 999,task_id: 'the-task',}- const existing: ChatMessage[] = [low!, bubble, high!]- const incoming = numbered+ const existing: ChatMessage[] = [low!, bubble, diskHigh]const activeTaskIds = active ? new Set(['the-task']) : new Set<string>()const merged = mergeAuthoritativeServerMessages(existing, incoming, { activeTaskIds })const ids = merged.map(m => m.id)- // Prop #7 liveness: an idle content-echo collapses; a live bubble (I-5) and- // a content-divergent orphan both survive (gate (b) — no local text lost).- if (isEcho && !active) {+ // Prop #7 liveness: ONLY an idle bubble whose content matches the AUTHORITATIVE+ // incoming row collapses. A live bubble (I-5), a content-divergent orphan, and+ // a stale-echo (matches the disk neighbour but not incoming) all survive —+ // gate (b) plus the authoritative-comparison fix: no local text is lost.+ if (bubbleMode === 'fresh-echo' && !active) {expect(ids).not.toContain('sandwiched-bubble')} else {expect(ids).toContain('sandwiched-bubble')+ // The surviving bubble keeps its original text in the output.+ expect(merged.find(m => m.id === 'sandwiched-bubble')?.content).toBe(bubbleContent)}+ // The authoritative higher turn always lands with its FRESH content.+ expect(merged.find(m => m.serverTurnNumber === start + 1 && m.role === role)?.content).toBe(+ high!.content+ )// Prop #1: no numbered server row is ever suppressed (R1-B1 guard).for (const server of incoming) {diff --git a/desktop-app/src/chatMessageMerge.ts b/desktop-app/src/chatMessageMerge.tsindex 58fd8471b..25ff1c963 100644--- a/desktop-app/src/chatMessageMerge.ts+++ b/desktop-app/src/chatMessageMerge.ts@@ -254,35 +254,60 @@ export function mergeAuthoritativeServerMessages(candidateTurns.add(messageServerTurnNumber(candidate)!)}- // Idle echo collapse, gated by content (§6.1, R2-H1). When no incoming server- // row of this role is free to host the idle optimistic (candidateTurns empty)- // AND it is bracketed on both sides by numbered turns (slot saturation — the- // core case is the consecutive same-role sandwich Q = P + 1), the bubble is- // the residual echo of an already-materialised numbered turn. We drop it- // (drop-only) ONLY when its content matches a numbered neighbour of the same- // role that is already in the output — so its text still survives in that- // numbered row and nothing observable is lost. We compare against `next` first+ // Idle echo collapse, gated by content against the AUTHORITATIVE incoming row+ // that fills the neighbour slot (§6.1, R2-H1). When no incoming server row of+ // this role is free to host the idle optimistic (candidateTurns empty) AND it is+ // bracketed on both sides by numbered turns (slot saturation — the core case is+ // the consecutive same-role sandwich Q = P + 1), the bubble is the residual echo+ // of an already-materialised numbered turn. We drop it (drop-only) ONLY when its+ // content matches the AUTHORITATIVE incoming row of the same role that fills the+ // neighbour's (turn, role) slot — NOT the numbered neighbour taken from+ // `existing`, which can be stale: the row that actually lands in that output slot+ // comes from `incoming` (the first loop replaces the disk row with it), and their+ // content can diverge for the same slot (the assistant `response` evolves via+ // streaming / server-side reformat). Comparing against the stale disk neighbour+ // gives a false-equal and drops a local whose text is NOT in the output. Comparing+ // against the incoming row that actually occupies the slot makes the drop safe by+ // construction: that row survives verbatim (prop #1; preferredServerMessage never+ // rewrites content), so L's text is present in the output. We check `next` first// (the higher turn, the R2-H1 echo semantics) and fall back to `prev`.//+ // We do NOT additionally require Q = P + 1: the content-vs-authoritative gate+ // already makes every drop loss-safe, so narrowing to strict adjacency closes no+ // loss path — and it WOULD reintroduce a permanent duplicate in the saturated-gap+ // case (I-2: Q > P+1 with every in-between slot already numbered), where the echo+ // must still collapse. So the branch stays general over `candidateTurns.size === 0`.+ //// The content gate is what makes this safe versus a content-blind positional// rule: an orphan of a task that never registered a server turn (cancelled-in-// queue, budget-denied, persistTurnStart failure — verified in mcp-host) stays// turnless idle non-durable and, with divergent content, is NOT collapsed, so- // no local text is lost. Declared residual FP: a user message whose content is- // identical to an adjacent same-role numbered turn AND is an orphan of a- // turn-less task is dropped — a rare loss of a duplicate-content message,- // unavoidable without an identity discriminator the server does not provide.+ // no local text is lost. Declared residual FP: a message whose content is+ // identical to the adjacent same-role AUTHORITATIVE turn AND is an orphan of a+ // turn-less task is dropped as a visual duplicate — but its text still survives+ // verbatim in that authoritative row, so no local text is lost. The residual FP+ // is only the collapse of a duplicate bubble, unavoidable without an identity+ // discriminator the server does not provide.if (candidateTurns.size === 0) {const previousNumbered = previousNumberedMessages[index]const nextNumbered = nextNumberedMessages[index]if (previousNumbered && nextNumbered) {- const echoNeighbour =- nextNumbered.role === message.role && nextNumbered.content === message.content- ? nextNumbered- : previousNumbered.role === message.role && previousNumbered.content === message.content- ? previousNumbered+ const authoritativeSlotRow = (neighbour: ChatMessage): ChatMessage | undefined => {+ const neighbourTurn = messageServerTurnNumber(neighbour)+ return authoritative.find(+ server =>+ server.role === message.role && messageServerTurnNumber(server) === neighbourTurn+ )+ }+ const nextAuthoritative = authoritativeSlotRow(nextNumbered)+ const previousAuthoritative = authoritativeSlotRow(previousNumbered)+ const echoRow =+ nextAuthoritative?.content === message.content+ ? nextAuthoritative+ : previousAuthoritative?.content === message.content+ ? previousAuthoritative: undefined- if (echoNeighbour) dropLocalEcho(message)+ if (echoRow) dropLocalEcho(message)}continue}