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): 预测排空队列后重查会话资格

Shinku75f360bab11b
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
index f2c588ef69..3f4568a7f6 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
@@ -29,6 +29,10 @@ const h = vi.hoisted(() => ({
),
/** 模拟 DB 返回的 session row */
sessionRow: { remoteHostId: null, agentKind: null } as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null } | undefined,
+ /** 模拟 DB 第二次读取(排空落盘队列之后)返回的 session row;用于「drain 期间被删除」竞态测试。 */
+ sessionRowAfterDrain: undefined as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null } | undefined,
+ /** DB 读取次数计数(区分 drain 前后的两次读取)。 */
+ dbReads: 0,
}));
function nativeImageEmpty() {
@@ -167,7 +171,14 @@ vi.mock('../../localDb/client/current.js', () => ({
drizzle: {
select: () => ({
from: () => ({
- where: () => Promise.resolve([h.sessionRow].filter(Boolean)),
+ where: () => {
+ h.dbReads += 1;
+ const row =
+ h.dbReads >= 2 && h.sessionRowAfterDrain !== undefined
+ ? h.sessionRowAfterDrain
+ : h.sessionRow;
+ return Promise.resolve([row].filter(Boolean));
+ },
}),
}),
},
@@ -212,6 +223,8 @@ beforeEach(() => {
h.handlers.clear();
h.trusted = true;
h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
+ h.sessionRowAfterDrain = undefined;
+ h.dbReads = 0;
h.predict.mockResolvedValue('下一步做什么');
registerMakerTitleIpc();
});
@@ -310,6 +323,16 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
});
+
+ it('drain 等待期间 session 被软删除时,排空后重新校验并静默返回 null', async () => {
+ // 第一次读取(drain 前)返回合法本地 session;排空落盘队列后第二次读取返回已删除状态,
+ // 覆盖 TOCTOU 竞态:drain 前的 status 检查过期后,必须在素材物化/调用 provider 前重新校验。
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
+ h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'cc', status: 'deleted' };
+
+ await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
+ expect(h.predict).not.toHaveBeenCalled();
+ });
});
describe('maker:predict-prompt — 多窗口去重', () => {
diff --git a/apps/desktop/src/main/maker-ipc/title.ts b/apps/desktop/src/main/maker-ipc/title.ts
index d5c7594033..566ea52776 100644
--- a/apps/desktop/src/main/maker-ipc/title.ts
+++ b/apps/desktop/src/main/maker-ipc/title.ts
@@ -481,6 +481,26 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}
// stale UI 可能携带其它会话转写或伪造 workdir,把非本会话内容送到本地 provider 触发
// 付费调用。先排空落盘队列,确保刚结束 turn 的最终回复已持久化,再读最近消息。
await drainPersistQueue();
+ // 重新校验 session 资格:drainPersistQueue 等待期间 session 可能被软删除或转为远程/review,
+ // 上方基于 status / source / remoteHostId 的防御纵深检查会过期(TOCTOU)。此处再从 DB 读取
+ // 同一行的资格字段,若已不再可预测(缺失 / 远程 / review / 已删除),静默返回 null,避免把
+ // 已删除或已易主的转写外发到本地 provider 触发付费调用。agentKind 不可变且已在上方校验,无需复查。
+ const [latestSessionRow] = await getDbClient()
+ .drizzle.select({
+ remoteHostId: sessions.remoteHostId,
+ source: sessions.source,
+ status: sessions.status,
+ })
+ .from(sessions)
+ .where(eq(sessions.id, sessionId));
+ if (
+ !latestSessionRow ||
+ latestSessionRow.remoteHostId ||
+ latestSessionRow.source === 'review' ||
+ latestSessionRow.status === 'deleted'
+ ) {
+ return { prompt: null };
+ }
const material = await regenerateTitleMaterial(sessionId, PREDICTION_RECENT_MESSAGE_LIMIT);
const messages = material.recent.map((m) => ({ role: m.role, content: m.text }));
return {