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(desktop): gate history mirror writes after review feedback

Leng Jingxu2a50b85c2e29
diff --git a/apps/desktop/src/main/device-link/__tests__/historyScanFallback.test.ts b/apps/desktop/src/main/device-link/__tests__/historyScanFallback.test.ts
index c876c86d7f..b566ad8d9b 100644
--- a/apps/desktop/src/main/device-link/__tests__/historyScanFallback.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/historyScanFallback.test.ts
@@ -27,8 +27,8 @@ describe.each(['\n', '\r\n'])('automatic scan budget fallback owners (%j)', (new
for (const current of [true, false]) {
reconcile.mockClear();
callback(desktopSource, ' view.subscribe(() => {', '\n });\n void view.restoreCachedView',
- ['isCurrent', 'sessions', 'sessionId', 'view', 'isHistoryViewUnavailable', 'reconcileRemoteMessages', 'persistTimer', 'writeCache', 'setState', 'mirrorScheduled', 'mirrorSnapshot'],
- [() => current, new Set(['s']), 's', view, isHistoryViewUnavailable, reconcile, undefined, undefined, vi.fn(), false, vi.fn()]);
+ ['isCurrent', 'sessions', 'sessionId', 'view', 'isHistoryViewUnavailable', 'reconcileRemoteMessages', 'persistTimer', 'writeCache', 'setState', 'mirrorScheduled', 'mirrorSnapshot', '_messagesEpoch', 'mirrorEpoch', 'mirrorLease', '_activeViewSessions'],
+ [() => current, new Set(['s']), 's', view, isHistoryViewUnavailable, reconcile, undefined, undefined, vi.fn(), false, vi.fn(), new Map(), 0, 0, new Map()]);
await Promise.resolve();
expect(reconcile).toHaveBeenCalledTimes(active && current ? 1 : 0);
if (active && current) expect(reconcile).toHaveBeenCalledWith('s', { force: true });
diff --git a/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts b/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts
index 99fecee475..71ed0a84f6 100644
--- a/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts
+++ b/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts
@@ -291,13 +291,16 @@ describe('device-link controller mirror — end-to-end scenarios', () => {
remoteProjectsStore.setDeviceSessions(DEVICE_ID, 'Mac A', [{ id: s } as Session]);
const leave = makerChatStore.enterView(s);
let off = () => {};
+ let offView = () => {};
try {
makerChatStore.ensureInitialMessages(s);
await flush();
const view = getRemoteHistoryView(s)!;
expect(view.getSnapshot().ready).toBe(true);
const notify = vi.fn();
+ const publishNotified: boolean[] = [];
off = makerChatStore.subscribe(s, notify);
+ offView = view.subscribe(() => publishNotified.push(notify.mock.calls.length > 0));
host.seedSession(s, {}, [
dbMessage(s, 'first', 'first', '2026-06-15T00:00:00Z'),
dbMessage(s, 'second', 'second', '2026-06-15T00:00:01Z'),
@@ -305,10 +308,39 @@ describe('device-link controller mirror — end-to-end scenarios', () => {
const read = view.refresh();
expect(notify).not.toHaveBeenCalled();
await read;
- await flush();
+ expect(publishNotified.length).toBeGreaterThanOrEqual(2);
+ expect(publishNotified.every((notified) => notified === false)).toBe(true);
expect(notify).toHaveBeenCalled();
expect(makerChatStore.getSnapshot(s).messages.map((message) => message.clientId))
.toEqual(['client-first', 'client-second']);
+ notify.mockClear();
+ await view.refresh();
+ await flush();
+ expect(notify).not.toHaveBeenCalled();
+ } finally {
+ offView();
+ off();
+ leave();
+ }
+ });
+
+ it('does not mirror a history publish after the view leaves', async () => {
+ const s = sid();
+ host.enableHistoryView();
+ host.seedSession(s, {}, [dbMessage(s, 'first', 'first', '2026-06-15T00:00:00Z')]);
+ remoteProjectsStore.setDeviceSessions(DEVICE_ID, 'Mac A', [{ id: s } as Session]);
+ const leave = makerChatStore.enterView(s);
+ const notify = vi.fn();
+ let off = () => {};
+ try {
+ makerChatStore.ensureInitialMessages(s);
+ await flush();
+ const view = getRemoteHistoryView(s)!;
+ off = makerChatStore.subscribe(s, notify);
+ leave();
+ await flush();
+ expect(view.isActive()).toBe(false);
+ expect(notify).not.toHaveBeenCalled();
} finally {
off();
leave();
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index abb97646ed..e32fd73757 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -11867,9 +11867,13 @@ function createRemoteHistoryView(sessionId: string) {
remoteHistoryViews.set(sessionId, entry);
view.setNetworkAvailable(!deviceId || !isRemoteDeviceMarkedDisconnected(deviceId));
let mirrorScheduled = false;
+ let mirrorEpoch = 0;
+ let mirrorLease = 0;
const mirrorSnapshot = () => {
mirrorScheduled = false;
if (!isCurrent() || !sessions.has(sessionId)) return;
+ if ((_activeViewSessions.get(sessionId) ?? 0) < mirrorLease) return;
+ if ((_messagesEpoch.get(sessionId) ?? 0) !== mirrorEpoch) return;
const snapshot = view.getSnapshot();
if (isHistoryViewUnavailable(snapshot.error)) {
// Keep the last usable mirror until the raw fallback succeeds and replaces it.
@@ -11888,9 +11892,13 @@ function createRemoteHistoryView(sessionId: string) {
const borrowed = deviceId && snapshot.stale ? available.map(message => ({ ...message,
cacheHydrated: true as const, cacheReceipt: snapshot.readReceipt })) : available;
setState(sessionId, (state) => {
+ const rawMessages = state.messages.some((message) => message.cacheHydrated
+ && !historyCacheReceiptFollows(message.cacheReceipt, snapshot.readReceipt, snapshot.stale))
+ ? state.messages.filter((message) => !message.cacheHydrated
+ || historyCacheReceiptFollows(message.cacheReceipt, snapshot.readReceipt, snapshot.stale))
+ : state.messages;
const messages = confirmRemoteUsers(
- mergeMessages(borrowed, state.messages.filter((message) => !message.cacheHydrated
- || historyCacheReceiptFollows(message.cacheReceipt, snapshot.readReceipt, snapshot.stale)), { addOnly: true }),
+ mergeMessages(borrowed, rawMessages, { addOnly: true }),
new Set(available.filter((message) => message.role === 'user').map((message) => message.clientId)),
);
const isLoadingMore = view.isLoadingOlder();
@@ -11898,11 +11906,10 @@ function createRemoteHistoryView(sessionId: string) {
&& state.hasMoreMessages === snapshot.hasMore
&& state.isLoadingMore === isLoadingMore
&& state.oldestMessageId === snapshot.nextCursor
- && state.historyWindowIslands.length === 0
? state
: { ...state, historyLoaded: true, messages,
hasMoreMessages: snapshot.hasMore, isLoadingMore,
- oldestMessageId: snapshot.nextCursor, historyWindowIslands: EMPTY_WINDOW_ISLANDS };
+ oldestMessageId: snapshot.nextCursor };
});
};
view.subscribe(() => {
@@ -11924,6 +11931,8 @@ function createRemoteHistoryView(sessionId: string) {
}
if (mirrorScheduled) return;
mirrorScheduled = true;
+ mirrorEpoch = _messagesEpoch.get(sessionId) ?? 0;
+ mirrorLease = _activeViewSessions.get(sessionId) ?? 0;
queueMicrotask(mirrorSnapshot);
});
void view.restoreCachedView(async () => {