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): defer history mirror writes after view publish

Leng Jingxub597c27db956
diff --git a/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts b/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts
index f6b6c96671..99fecee475 100644
--- a/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts
+++ b/apps/desktop/src/renderer/__tests__/deviceLinkControllerScenarios.test.ts
@@ -284,6 +284,37 @@ describe('device-link controller mirror — end-to-end scenarios', () => {
},
);
+ it('defers the history-view mirror write out of the publish stack', 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);
+ let off = () => {};
+ try {
+ makerChatStore.ensureInitialMessages(s);
+ await flush();
+ const view = getRemoteHistoryView(s)!;
+ expect(view.getSnapshot().ready).toBe(true);
+ const notify = vi.fn();
+ off = makerChatStore.subscribe(s, notify);
+ host.seedSession(s, {}, [
+ dbMessage(s, 'first', 'first', '2026-06-15T00:00:00Z'),
+ dbMessage(s, 'second', 'second', '2026-06-15T00:00:01Z'),
+ ]);
+ const read = view.refresh();
+ expect(notify).not.toHaveBeenCalled();
+ await read;
+ await flush();
+ expect(notify).toHaveBeenCalled();
+ expect(makerChatStore.getSnapshot(s).messages.map((message) => message.clientId))
+ .toEqual(['client-first', 'client-second']);
+ } finally {
+ off();
+ leave();
+ }
+ });
+
// A durable push both invalidates the view (page read at +500 ms) and arms the
// lost-push repair (+1.5 s). The repair must only read when nothing read the
// latest page after the push: a running turn used to cost two page reads plus an
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 4a55388c2d..ceda31a878 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -11866,41 +11866,63 @@ function createRemoteHistoryView(sessionId: string) {
entry.isCurrent = isCurrent;
remoteHistoryViews.set(sessionId, entry);
view.setNetworkAvailable(!deviceId || !isRemoteDeviceMarkedDisconnected(deviceId));
- view.subscribe(() => {
- if (persistTimer) clearTimeout(persistTimer);
+ let mirrorScheduled = false;
+ const mirrorSnapshot = () => {
+ mirrorScheduled = false;
if (!isCurrent() || !sessions.has(sessionId)) return;
const snapshot = view.getSnapshot();
if (isHistoryViewUnavailable(snapshot.error)) {
- writeCache = undefined;
// Keep the last usable mirror until the raw fallback succeeds and replaces it.
- setState(sessionId, (state) => ({ ...state, messages: confirmRemoteUsers(state.messages,
- new Set(state.messages.filter((row) => !row.isPendingPersist).map((row) => row.clientId))) }));
- } else if (snapshot.ready && !snapshot.loading && !snapshot.error && writeCache) {
- const writer = writeCache;
- persistTimer = setTimeout(() => { if (isCurrent()) writer(snapshot); }, 1200);
- }
- if (!snapshot.ready) {
- if (view.isActive() && isHistoryViewUnavailable(snapshot.error)) {
+ setState(sessionId, (state) => {
+ const messages = confirmRemoteUsers(state.messages,
+ new Set(state.messages.filter((row) => !row.isPendingPersist).map((row) => row.clientId)));
+ return messages === state.messages ? state : { ...state, messages };
+ });
+ if (view.isActive()) {
void reconcileRemoteMessages(sessionId, { force: true }).catch(() => undefined);
}
return;
}
+ if (!snapshot.ready) return;
const available = historyViewLeaves(snapshot.items).flatMap((item) => item.type === 'messages' ? item.messages : []);
for (const detail of snapshot.details.values()) available.push(...detail.messages);
// Restored page rows are cached too. Keep their read order when borrowing them
// into the raw store, so a delayed disk receipt is comparable without treating live rows as old.
const borrowed = deviceId && snapshot.stale ? available.map(message => ({ ...message,
cacheHydrated: true as const, cacheReceipt: snapshot.readReceipt })) : available;
- setState(sessionId, (state) => ({ ...state, historyLoaded: true,
- messages: confirmRemoteUsers(
+ setState(sessionId, (state) => {
+ const messages = confirmRemoteUsers(
mergeMessages(borrowed, state.messages.filter((message) => !message.cacheHydrated
|| historyCacheReceiptFollows(message.cacheReceipt, snapshot.readReceipt, snapshot.stale)), { addOnly: true }),
new Set(available.filter((message) => message.role === 'user').map((message) => message.clientId)),
- ),
- hasMoreMessages: snapshot.hasMore, isLoadingMore: view.isLoadingOlder(),
- oldestMessageId: snapshot.nextCursor,
- historyWindowHasIsland: false,
- }));
+ );
+ const isLoadingMore = view.isLoadingOlder();
+ return state.historyLoaded && state.messages === messages
+ && 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 };
+ });
+ };
+ view.subscribe(() => {
+ if (persistTimer) clearTimeout(persistTimer);
+ if (!isCurrent() || !sessions.has(sessionId)) return;
+ const snapshot = view.getSnapshot();
+ const isLoadingMore = view.isLoadingOlder();
+ setState(sessionId, (state) => state.isLoadingMore === isLoadingMore
+ ? state : { ...state, isLoadingMore });
+ if (isHistoryViewUnavailable(snapshot.error)) writeCache = undefined;
+ else if (snapshot.ready && !snapshot.loading && !snapshot.error && writeCache) {
+ const writer = writeCache;
+ persistTimer = setTimeout(() => { if (isCurrent()) writer(snapshot); }, 1200);
+ }
+ if (mirrorScheduled) return;
+ mirrorScheduled = true;
+ queueMicrotask(mirrorSnapshot);
});
void view.restoreCachedView(async () => {
const snapshot = await cached;