Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): retain stream exclusions across history replays

xd63067b61cb77
diff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
index 093c33ab77..3d3b3637f5 100644
--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts
+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
@@ -888,7 +888,7 @@ describe('remoteSessionStore', () => {
} finally { vi.useRealTimers(); }
});
- it.each(['db-row', 'history'] as const)('keeps anonymous B when initially unseen old A arrives before its final (%s)', (arrival) => {
+ it.each([['db-row', 'item-a'], ['db-row', 'host-a'], ['history', 'item-a'], ['history', 'host-a']] as const)('keeps anonymous B when initially unseen old A arrives before its final (%s, %s)', (arrival, finalId) => {
vi.useFakeTimers();
try {
pushMakerText('s1', undefined, 'Sure', false);
@@ -902,11 +902,20 @@ describe('remoteSessionStore', () => {
} else {
remoteSessionStore.setLatestMessageWindow('s1', [older]);
}
+ // An equal DB replay must not retire B before the full correction either.
+ remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
+ sessionId: 's1', message: older,
+ });
remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
- sessionId: 's1', persistId: 'item-a', event: {
+ sessionId: 's1', persistId: finalId, event: {
type: 'text', data: { text: 'Corrected older reply', isFinal: true, isFullText: true },
},
});
+ const corrected = { ...older, content: 'Corrected older reply' };
+ remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
+ sessionId: 's1', message: corrected,
+ });
+ remoteSessionStore.setLatestMessageWindow('s1', [corrected]);
pushMakerText('s1', undefined, ' tail', false);
vi.runOnlyPendingTimers();
expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
@@ -973,6 +982,35 @@ describe('remoteSessionStore', () => {
} finally { vi.useRealTimers(); }
});
+ it.each([false, true, 'db-first'] as const)('keeps a known older row from claiming a matching anonymous stream through history (cropped=%s)', (cropped) => {
+ vi.useFakeTimers();
+ try {
+ const older = { ...message('host-a', 's1'), clientId: 'item-a', content: 'partial answer from earlier' };
+ if (cropped !== 'db-first') remoteSessionStore.setMessages('s1', [older]);
+ pushMakerText('s1', undefined, cropped === 'db-first' ? 'partial' : 'partial answer', false);
+ vi.runOnlyPendingTimers();
+ const newer = remoteSessionStore.getMessages('s1').find((row) => row.clientId?.startsWith('mobile-stream-'))!;
+ if (cropped === 'db-first') {
+ remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', { sessionId: 's1', message: older });
+ pushMakerText('s1', undefined, ' answer', false);
+ vi.runOnlyPendingTimers();
+ }
+ if (cropped) remoteSessionStore.setMessages('s1', [remoteSessionStore.getMessages('s1').find((row) => row.clientId === newer.clientId)!]);
+ remoteSessionStore.setLatestMessageWindow('s1', [older]);
+ remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
+ sessionId: 's1', persistId: 'item-a', event: {
+ type: 'text', data: { text: 'Corrected older reply', isFinal: true, isFullText: true },
+ },
+ });
+ pushMakerText('s1', undefined, ' tail', false);
+ vi.runOnlyPendingTimers();
+ expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
+ expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === newer.clientId))
+ .toMatchObject({ content: 'partial answer tail', agentMeta: { isStreaming: true } });
+ expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === 'item-a')?.content).toBe('Corrected older reply');
+ } finally { vi.useRealTimers(); }
+ });
+
it.each(['Sure, checked', 'Corrected'])('migrates a short anonymous stream when an identified delta follows its DB row (%s)', (finalText) => {
vi.useFakeTimers();
try {
diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.ts
index 810806c738..0ab916910f 100644
--- a/apps/mobile/src/session/remoteSessionStore.ts
+++ b/apps/mobile/src/session/remoteSessionStore.ts
@@ -625,9 +625,9 @@ const sessionTaskUpdates = new Map<string, ReadonlyMap<string, AgentTaskUpdate>>
// Keep one temporary assistant row per session and reconcile it with the persisted row by
// clientId/persistId when the database push arrives.
const streamingAssistantClientIds = new Map<string, string>();
-// A final for an item already present when an anonymous stream began cannot
-// claim that newer stream. Keep the observation boundary until its id resolves.
-const generatedStreamPriorIdentities = new Map<string, ReadonlySet<string>>();
+// Identities known to be separate from the current anonymous stream: rows present
+// at its start, plus persisted rows kept separate from it. Retain until it resolves.
+const generatedStreamPriorIdentities = new Map<string, Set<string>>();
// The same persisted streaming identity can be replayed by a stale transport after
// re-link. Track the transport currently assembling that identity so a device switch
// replaces the stale live text instead of concatenating two transports into one row.
@@ -2548,6 +2548,12 @@ function migrateGeneratedStreamingClientId(sessionId: string, generatedClientId:
return true;
}
+function excludeFromGeneratedStream(sessionId: string, ...ids: (string | undefined)[]): void {
+ const identities = generatedStreamPriorIdentities.get(sessionId);
+ if (!identities) return;
+ for (const id of ids) if (id) identities.add(id);
+}
+
function streamingClientIdFor(sessionId: string, persistId: string | undefined): StreamingClientIdResolution {
const normalizedPersistId = persistId?.trim();
if (normalizedPersistId) {
@@ -2589,7 +2595,7 @@ function upsertMessage(
): boolean {
const existing = messages.get(sessionId) ?? [];
const priorIdentities = generatedStreamPriorIdentities.get(sessionId);
- const preservesGeneratedStream = options.preserveGeneratedStreamingFallback === true
+ let preservesGeneratedStream = options.preserveGeneratedStreamingFallback === true
|| priorIdentities?.has(message.id) === true
|| (message.clientId ? priorIdentities?.has(message.clientId) === true : false);
const candidateIndex = options.knownIndex ?? -1;
@@ -2617,6 +2623,12 @@ function upsertMessage(
return true;
}
}
+ if (isPersistedAssistantMessage(message)
+ && isGeneratedStreamingClientId(streamingAssistantClientIds.get(sessionId))) {
+ // This row did not merge with the live fallback. Keep that separation on replay.
+ excludeFromGeneratedStream(sessionId, message.id, message.clientId);
+ preservesGeneratedStream = true;
+ }
if (index < 0) {
messages.set(sessionId, normalizeMessages([...existing, message]));
applyMessageWriteRetention(sessionId);
@@ -3885,7 +3897,11 @@ export const remoteSessionStore = {
if (isPersistedAssistantMessage(item)) {
const fallbackIndex = findPendingGeneratedStreamingFallbackIndex(sessionId, existing);
const fallback = fallbackIndex >= 0 ? existing[fallbackIndex] : undefined;
- if (fallback && generatedFallbackMatchesPersistedMessage(fallback, item)) {
+ const priorIdentities = generatedStreamPriorIdentities.get(sessionId);
+ const knownSeparateIdentity = priorIdentities?.has(item.id) === true
+ || (item.clientId ? priorIdentities?.has(item.clientId) === true : false)
+ || messageIdentityIndexHas(existingIdentityIndex, item);
+ if (fallback && !knownSeparateIdentity && generatedFallbackMatchesPersistedMessage(fallback, item)) {
// History sync may be the first place the authoritative DB identity
// arrives. Remove the generated key before inserting the persisted row;
// otherwise the newer temporary row is kept as a tail and the DB row
@@ -3905,6 +3921,7 @@ export const remoteSessionStore = {
continue;
}
}
+ if (isPersistedAssistantMessage(item)) excludeFromGeneratedStream(sessionId, item.id, item.clientId);
const directKey = messageKey(item);
const identityKey = findMessageMergeKey(byKey, item);
const key = identityKey ?? directKey;