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(bots): 旧页贴回的补读一次只发一次,等刷新结果落地再继续

Dash8c4531cf8204
diff --git a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
index 071d2e650c..69c6e4fa71 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
@@ -17,7 +17,11 @@ export interface ThreadMessage {
content: Array<{ type: string; text?: string; fallback?: string; namespace?: string; schemaRevision?: number; data?: Record<string, unknown>; mediaId?: string; caption?: string }>;
}
-/** Concurrent reads per refresh; the rest wait for a slot rather than fan out. */
+/**
+ * Concurrent reads per fill (a refresh fills previews and share sources separately); the rest
+ * wait for a slot rather than fan out. Reads are deduplicated per item while in flight, and a
+ * fill only covers the open page, so this bounds a refresh without a projection-wide queue.
+ */
const READ_CONCURRENCY = 6;
/** A failed read backs off from this delay, doubling, and retries on its own a few times. */
const RETRY_MS = 30_000;
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
index 2196747f41..e988fede0d 100644
--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
@@ -196,6 +196,8 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
const [planPending, setPlanPending] = useState<PlanPending | null>(null);
const planPendingRef = useRef(false);
const loadRef = useRef<(additionalSources?: string[]) => void>(() => {});
+ /** Refresh results applied so far; share follow-ups wait for the next one. */
+ const loadResults = useRef(0);
const scrollRef = useRef<HTMLDivElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const stickToBottomRef = useRef(true);
@@ -251,6 +253,7 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
olderHasMore: result.group.hasMoreBefore,
},
);
+ loadResults.current += 1;
const shown = stateRef.current;
if (shown.kind === 'ready') {
const targets = quotedOlderOriginals(shown.older, result.group.messages);
@@ -330,15 +333,19 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
// Older shares checked before the latest edit, deletion or reset are re-checked from the
// committed timeline, whichever of a refresh and an older page landed last. One refresh
// checks up to the per-read cap, so a bounded number of follow-ups reaches all of them.
- // Each version gets its own budget: an edit mid-way makes every card stale again.
- const shareFollowUps = useRef<{ version: number | undefined; count: number }>({ version: undefined, count: 0 });
+ // Each version gets its own budget: an edit mid-way makes every card stale again. One
+ // follow-up at a time: other commits (an older page, a quote check) wait until a refresh
+ // result lands instead of spending the budget on reads that would replace it.
+ const shareFollowUps = useRef<{ version: number | undefined; count: number; awaiting: number | null }>({ version: undefined, count: 0, awaiting: null });
useEffect(() => {
const version = state.kind === 'ready' ? state.group.threadShareVersion : undefined;
- if (shareFollowUps.current.version !== version) shareFollowUps.current = { version, count: 0 };
+ if (shareFollowUps.current.version !== version) shareFollowUps.current = { version, count: 0, awaiting: null };
if (state.kind !== 'ready' || !staleBotGroupShares(state.older, version).length) { shareFollowUps.current.count = 0; return; }
+ if (shareFollowUps.current.awaiting === loadResults.current) return;
const total = state.older.filter(message => message.threadShare).length;
if (shareFollowUps.current.count >= Math.ceil(total / BOT_GROUP_SHOWN_SHARES_MAX) + 1) return;
shareFollowUps.current.count += 1;
+ shareFollowUps.current.awaiting = loadResults.current;
loadRef.current();
}, [state]);
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
index 50e31ee0c7..01623e257a 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
@@ -2524,6 +2524,29 @@ describe('shares on older pages', () => {
await settle();
expect(mocks.getBotGroup.mock.calls.length - afterFirstEdit).toBeGreaterThan(1);
});
+
+ it('waits for a follow-up before another, whatever else commits meanwhile', async () => {
+ const share = { messageId: 'old-reply', threadRootId: 'old-root', status: 'available' as const, authorKind: 'bot' as const, authorBotId: 'xiaoman', authorName: '小满',
+ sharedByAuthor: false, content: '原文', attachments: [], createdAt: 1, edited: false, version: 1 };
+ const oldShare = msg({ id: 'old-share', sequence: 20, authorKind: 'user', isSelf: false, authorBotId: 'alice-actor', authorName: 'Alice', content: '↪ 卡片文字', threadShare: share });
+ const latest = detail({ serverBacked: true, hasMoreBefore: true, threadShareVersion: 2, messages: [msg({ id: 'new', sequence: 50, content: '最新' })] });
+ const followUps: Array<() => void> = [];
+ mocks.getBotGroup.mockImplementation((_groupId: string, options?: { beforeSequence?: number; shownShares?: unknown[] }) => {
+ if (options?.beforeSequence === 50) return Promise.resolve({ ok: true, group: { ...latest, messages: [oldShare] } });
+ if (options?.beforeSequence) return Promise.resolve({ ok: true, group: { ...latest, hasMoreBefore: false, messages: [msg({ id: 'oldest', sequence: 1, content: '更早' })] } });
+ // A follow-up checking the stale share stays in flight until released.
+ if (options?.shownShares?.length) return new Promise(resolve => followUps.push(() => resolve({ ok: true, group: latest })));
+ return Promise.resolve({ ok: true, group: latest });
+ });
+ renderView();
+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ await waitFor(() => expect(followUps).toHaveLength(1));
+ // Another older page lands while that follow-up is still in flight.
+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ await screen.findByText('更早');
+ await act(async () => { await Promise.resolve(); });
+ expect(followUps).toHaveLength(1);
+ });
});
describe('more older shares than one refresh re-reads', () => {