fix(sync): remove stale-delta skip and add parts-gap recovery (#889)

The pipeline's stale-delta mechanism incorrectly marked all
message.part.delta events as stale when a message.part.updated
coalesced, regardless of queue position. This caused valid streaming
deltas to be silently dropped, resulting in blank or incomplete
assistant messages.

Additionally, when part events were dropped by the reducer (missing
parts array or partID not found), there was no recovery path — the
state stayed permanently out of sync until the next SSE reconnect
or manual refresh.

Also discovered: message.updated that successfully writes an assistant
message but has empty parts would render a blank bubble, with no
repair triggered since repair only ran on reducer return false.

Changes:
- Remove staleDeltas Set and deltaKey from event-pipeline.ts
- Coalesce still replaces same-key events, but deltas are never skipped
- Add enqueuePartsRepair + repairSessionParts to sync-context.tsx
  (5s cooldown, deduped, async SDK re-fetch)
- Trigger repair on reducer return false for part events
- Trigger repair on message.updated return true with empty parts
- Add sync debug.ts with gated diagnostic logging
- Add pipeline coalescing tests
This commit is contained in:
jwcrystal
2026-04-11 23:40:47 +03:00
committed by GitHub
parent 1c7a0b8ddd
commit 964c209ff6
5 changed files with 351 additions and 18 deletions
+15 -3
View File
@@ -14,6 +14,7 @@ import { Binary } from "./binary"
import type { GlobalState, State } from "./types"
import { dropSessionCaches } from "./session-cache"
import { stripSessionDiffSnapshots } from "./sanitize"
import { syncDebug } from "./debug"
const SKIP_PARTS = new Set(["patch", "step-start", "step-finish"])
@@ -160,6 +161,7 @@ export function applyDirectoryEvent(
&& (existing as { finish?: unknown }).finish === (info as { finish?: unknown }).finish
&& (existing.time as { completed?: number })?.completed === (info.time as { completed?: number })?.completed
if (unchanged) {
syncDebug.reducer.messageUpdatedUnchanged(info.sessionID, info.id, info.role, (info as { finish?: unknown }).finish, (info.time as { completed?: number })?.completed)
return false
}
const next = [...messages]
@@ -190,10 +192,14 @@ export function applyDirectoryEvent(
case "message.part.updated": {
const part = (event.properties as { part: Part }).part
if (SKIP_PARTS.has(part.type)) return false
if (SKIP_PARTS.has(part.type)) {
syncDebug.reducer.partSkipped((part as { messageID: string }).messageID, part.id, part.type)
return false
}
const messageID = (part as { messageID: string }).messageID
const parts = draft.part[messageID]
if (!parts) {
syncDebug.reducer.partUpdatedNoExistingParts(messageID, part.id, part.type)
draft.part[messageID] = [part]
return true
}
@@ -246,9 +252,15 @@ export function applyDirectoryEvent(
delta: string
}
const parts = draft.part[props.messageID]
if (!parts) return false
if (!parts) {
syncDebug.reducer.partDeltaNoParts(props.messageID, props.partID)
return false
}
const result = Binary.search(parts, props.partID, (p) => p.id)
if (!result.found) return false
if (!result.found) {
syncDebug.reducer.partDeltaNotFound(props.messageID, props.partID)
return false
}
const existing = parts[result.index] as Record<string, unknown>
const existingValue = existing[props.field] as string | undefined
// Create new Part object + new array so React detects the change