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): 迟到的话题读取不再覆盖已失效的缓存

Dash4f974af92506
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 d7b3997b36..4dd2b1e465 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
@@ -141,3 +141,38 @@ describe('reply summaries', () => {
expect(isShareableThreadReply(reply('n', rootA, { content: [{ type: 'card', namespace: 'cindy.group-notice', fallback: 'x' }] }))).toBe(false);
});
});
+
+describe('late reads after a reset or invalidation', () => {
+ const deferred = <T,>() => { let resolve!: (value: T) => void; const promise = new Promise<T>(r => { resolve = r; }); return { promise, resolve }; };
+ const sourceId = '40000000-0000-4000-8000-000000000001';
+ const share = message({ id: 's1', content: [buildBotGroupThreadShareCard({ messageId: sourceId, threadRootId: rootA, authorName: 'Miao', text: 'hi' })] });
+
+ it.each(['reset', 'invalidate'] as const)('does not let a thread page started before %s refill the cache', async (clear) => {
+ const io = { threadPage: vi.fn(), message: vi.fn() };
+ const projection = createThreadProjection(io);
+ const root = message({ id: rootB, replyCount: 1 });
+ const stale = deferred<ThreadMessage[]>();
+ io.threadPage.mockReturnValueOnce(stale.promise).mockResolvedValue([reply('b1', rootB, { revision: 2 })]);
+ const inFlight = projection.previews(room, [root], fullWindow([]));
+ if (clear === 'reset') projection.reset(); else projection.invalidate(room, 'b1');
+ stale.resolve([reply('b1', rootB)]);
+ await inFlight;
+ const next = await projection.previews(room, [root], fullWindow([]));
+ expect(io.threadPage).toHaveBeenCalledTimes(2);
+ expect(next.get(rootB)![0]!.revision).toBe(2);
+ });
+
+ it.each(['reset', 'invalidate'] as const)('does not let a share source read started before %s refill the cache', async (clear) => {
+ const io = { threadPage: vi.fn(), message: vi.fn() };
+ const projection = createThreadProjection(io);
+ const stale = deferred<ThreadMessage | null>();
+ io.message.mockReturnValueOnce(stale.promise).mockResolvedValue(reply(sourceId, rootA, { revision: 2 }));
+ const inFlight = projection.shareSources(room, [share], []);
+ if (clear === 'reset') projection.reset(); else projection.invalidate(room, sourceId);
+ stale.resolve(reply(sourceId, rootA));
+ await inFlight;
+ const next = await projection.shareSources(room, [share], []);
+ expect(io.message).toHaveBeenCalledTimes(2);
+ expect(next.get(sourceId)?.revision).toBe(2);
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
index cf8987967f..7536cf4db1 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
@@ -41,6 +41,8 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
const previews = new Map<string, { replyCount: number; replies: M[] }>();
const sources = new Map<string, M | null>();
const retryAt = new Map<string, number>();
+ // A read that was in flight across reset()/invalidate() may carry the old copy.
+ let generation = 0;
const key = (roomId: string, id: string) => `${roomId}:${id}`;
const waiting = (cacheKey: string) => (retryAt.get(cacheKey) ?? 0) > Date.now();
@@ -71,9 +73,10 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
}
await eachLimited(missing, async root => {
const cacheKey = key(roomId, root.id);
+ const started = generation;
try {
const replies = (await io.threadPage(roomId, root.id, FALLBACK_PAGE)).filter(message => !message.deleted);
- previews.set(cacheKey, { replyCount: root.replyCount ?? 0, replies });
+ if (generation === started) previews.set(cacheKey, { replyCount: root.replyCount ?? 0, replies });
retryAt.delete(cacheKey);
result.set(root.id, replies);
} catch { retryAt.set(cacheKey, Date.now() + RETRY_MS); }
@@ -100,9 +103,11 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
}
await eachLimited([...missing], async messageId => {
const cacheKey = key(roomId, messageId);
+ const started = generation;
try {
const value = await io.message(roomId, messageId);
- sources.set(cacheKey, value); retryAt.delete(cacheKey);
+ if (generation === started) sources.set(cacheKey, value);
+ retryAt.delete(cacheKey);
result.set(messageId, value);
} catch { retryAt.set(cacheKey, Date.now() + RETRY_MS); }
});
@@ -110,12 +115,13 @@ 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;
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() { previews.clear(); sources.clear(); retryAt.clear(); },
+ reset() { generation += 1; previews.clear(); sources.clear(); retryAt.clear(); },
};
}