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): 话题预览与贴回来源的作废代次按群各自计算

Dash1ddec6367713
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
index 8b90a8e086..22330610c9 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
@@ -230,6 +230,28 @@ describe('late reads after a reset or invalidation', () => {
expect(io.message).toHaveBeenCalledTimes(2);
expect(projection.shareSources(room, [share], []).get(sourceId)?.id).toBe(sourceId);
});
+
+ it('keeps reads in one room when another room is edited', async () => {
+ const io = makeIo();
+ const projection = createThreadProjection(io);
+ const otherRoom = '10000000-0000-4000-8000-000000000002';
+ const pending = deferred<ThreadMessage | null>();
+ io.message.mockReturnValueOnce(pending.promise);
+ const before = projection.version(room);
+ projection.shareSources(room, [share], []);
+ projection.invalidate(otherRoom, 'elsewhere');
+ expect(projection.version(room)).toBe(before);
+ expect(projection.version(otherRoom)).toBeGreaterThan(before);
+ pending.resolve(reply(sourceId, rootA));
+ await projection.settled();
+ expect(io.message).toHaveBeenCalledTimes(1);
+ expect(projection.shareSources(room, [share], []).get(sourceId)?.id).toBe(sourceId);
+ // A reset still raises every room.
+ const edited = projection.version(otherRoom);
+ projection.reset();
+ expect(projection.version(room)).toBeGreaterThan(before);
+ expect(projection.version(otherRoom)).toBeGreaterThan(edited);
+ });
});
describe('reply summaries', () => {
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index d9cd356e5c..2f9a9aae37 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -636,7 +636,7 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
async function decorateThreads(roomId: string, views: BotGroupMessageView[], page: Message[], snapshot: Message[], members: Member[], withPreviews: boolean,
shownShares: Array<{ id: string; messageId: string; threadRootId: string; sharerId?: string }> = []) {
// Taken before reading, so a change while this read is in progress marks it stale.
- const version = threads.version();
+ const version = threads.version(roomId);
const previews = withPreviews ? threads.previews(roomId, page, snapshot) : new Map<string, Message[]>();
const sources = threads.sources(roomId, [...page.flatMap(m => readBotGroupThreadShareCard(m)?.messageId ?? []),
...shownShares.map(share => share.messageId)], [...snapshot, ...page]);
diff --git a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
index 69c6e4fa71..9ab2cf27fc 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
@@ -99,15 +99,19 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
retryTimers.clear(); pendingRetries.clear(); failures.clear();
}
- // A read that was in flight across reset()/invalidate() may carry the old copy.
- let generation = 0;
+ // A read that was in flight across reset()/invalidate() may carry the old copy. Edits only
+ // raise their own room, so a busy group never discards another group's reads; the sum of
+ // both counters only grows, which keeps a room's version ordered.
+ let epoch = 0;
+ const roomGenerations = new Map<string, number>();
+ const generation = (roomId: string) => epoch + (roomGenerations.get(roomId) ?? 0);
const key = (roomId: string, id: string) => `${roomId}:${id}`;
const waiting = (cacheKey: string) => inFlight.has(cacheKey) || (retryAt.get(cacheKey) ?? 0) > Date.now();
function fill<T>(roomId: string, items: Array<{ cacheKey: string; item: T }>, read: (item: T) => Promise<(() => void) | null>) {
if (!items.length) return;
for (const { cacheKey } of items) inFlight.add(cacheKey);
- const started = generation;
+ const started = generation(roomId);
const task = (async () => {
let wrote = false;
await eachLimited(items, async ({ cacheKey, item }) => {
@@ -116,8 +120,8 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
try {
const store = await read(item);
retryAt.delete(cacheKey); settledRetry(roomId, cacheKey);
- if (store) { if (generation === started) store(); wrote = true; }
- } catch { if (generation === started) scheduleRetry(roomId, cacheKey); else wrote = true; }
+ if (store) { if (generation(roomId) === started) store(); wrote = true; }
+ } catch { if (generation(roomId) === started) scheduleRetry(roomId, cacheKey); else wrote = true; }
finally { inFlight.delete(cacheKey); }
});
if (wrote) io.filled(roomId);
@@ -195,17 +199,17 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
},
/** An edited or deleted message may be a cached reply or share source. */
invalidate(roomId: string, messageId: string) {
- generation += 1;
+ roomGenerations.set(roomId, (roomGenerations.get(roomId) ?? 0) + 1);
sources.delete(key(roomId, messageId));
for (const [cacheKey, entry] of previews)
if (cacheKey.startsWith(`${roomId}:`) && entry.replies.some(reply => reply.id === messageId)) previews.delete(cacheKey);
},
/** Pushes may have been missed while disconnected. */
- reset() { generation += 1; previews.clear(); sources.clear(); retryAt.clear(); cancelRetries(); },
+ reset() { epoch += 1; previews.clear(); sources.clear(); retryAt.clear(); cancelRetries(); },
/** The adapter is gone (account switch or quit): no wake-up may outlive it. */
- dispose() { generation += 1; cancelRetries(); },
- /** Raised by every invalidation and reset; views checked at an older one may be stale. */
- version() { return generation; },
+ dispose() { epoch += 1; cancelRetries(); },
+ /** Raised by every reset and by invalidations in this room; views checked at an older one may be stale. */
+ version(roomId: string) { return generation(roomId); },
/** Tests: wait for background fills started so far. */
async settled() { while (settling.size) await Promise.all([...settling]); },
};