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): 跨窗口去重使用 DB updatedAt 替代 renderer turnGen

Shinku897af3acbb24
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 0fb3c17ca6..aedc26d56b 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
@@ -28,9 +28,9 @@ 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,
+ sessionRow: { remoteHostId: null, agentKind: null, updatedAt: 1 } as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null; updatedAt?: number } | undefined,
/** 模拟 DB 第二次读取(排空落盘队列之后)返回的 session row;用于「drain 期间被删除」竞态测试。 */
- sessionRowAfterDrain: undefined as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null } | undefined,
+ sessionRowAfterDrain: undefined as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null; updatedAt?: number } | undefined,
/** DB 读取次数计数(区分 drain 前后的两次读取)。 */
dbReads: 0,
}));
@@ -223,7 +223,7 @@ beforeEach(() => {
vi.clearAllMocks();
h.handlers.clear();
h.trusted = true;
- h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };
h.sessionRowAfterDrain = undefined;
h.dbReads = 0;
h.predict.mockResolvedValue('下一步做什么');
@@ -268,7 +268,7 @@ describe('maker:predict-prompt — payload 运行期校验', () => {
});
it('素材从 DB 读取,不采用 renderer 上报的 messages / workingDir', async () => {
- h.sessionRow = { remoteHostId: null, agentKind: 'pi', workingDir: '/db/workdir' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'pi', workingDir: '/db/workdir', updatedAt: 1 };
await invokePredict({
sessionId: 'session-1',
agentKind: 'pi',
@@ -302,14 +302,14 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
});
it('session 为远程会话(remoteHostId 非空)时静默返回 null', async () => {
- h.sessionRow = { remoteHostId: 'ssh-host-1', agentKind: 'cc' };
+ h.sessionRow = { remoteHostId: 'ssh-host-1', agentKind: 'cc', updatedAt: 1 };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
});
it('本地 session(remoteHostId=null, agentKind 匹配)正常执行预测', async () => {
- h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: '下一步做什么' });
expect(h.predict).toHaveBeenCalledTimes(1);
@@ -317,14 +317,14 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
it('agentKind 不匹配时静默返回 null,不触发付费调用', async () => {
// renderer 上报 claude-code,DB 存的是 codex
- h.sessionRow = { remoteHostId: null, agentKind: 'codex' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'codex', updatedAt: 1 };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
});
it('session 为已删除(soft-deleted)时静默返回 null,不触发付费调用', async () => {
- h.sessionRow = { remoteHostId: null, agentKind: 'cc', status: 'deleted' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', status: 'deleted', updatedAt: 1 };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
@@ -333,8 +333,8 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
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' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };
+ h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'cc', status: 'deleted', updatedAt: 1 };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
@@ -345,8 +345,8 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
// agentKind='codex'。sessionAgentSwitchHandler 会在会话切换时提交 agentKind 变更,因此
// drain 前的 agentKind 校验已过期:必须在调用 provider 前用 drain 后的 DB agentKind 复核,
// 与 renderer 上报(claude-code)不一致时拒绝,避免把转写路由到切换前的 provider/账号。
- h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
- h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'codex' };
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };
+ h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'codex', updatedAt: 1 };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
@@ -354,7 +354,7 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
});
describe('maker:predict-prompt — 多窗口去重', () => {
- it('同一 session 同一 turnGen 并发调用时第二个请求直接返回 null,避免重复付费', async () => {
+ it('同一 session 同一 updatedAt 并发调用时第二个请求直接返回 null,避免重复付费', async () => {
// 让第一次预测暂挂,模拟并发
let resolveFirst!: (value: string) => void;
h.predict.mockImplementationOnce(
@@ -374,18 +374,19 @@ describe('maker:predict-prompt — 多窗口去重', () => {
await expect(first).resolves.toEqual({ prompt: '预测结果' });
});
- it('同一 session 新 turnGen 的请求替换旧请求(旧轮结果终将被 renderer turnGen 校验丢弃)', async () => {
+ it('同一 session 新 updatedAt 的请求替换旧请求(旧轮结果终将被 renderer turnGen 校验丢弃)', async () => {
let resolveFirst!: (value: string) => void;
h.predict.mockImplementationOnce(
() => new Promise<string>((resolve) => { resolveFirst = resolve; }),
);
- // 发起 turnGen=0 的预测并暂挂
+ // 发起 updatedAt=1 的预测并暂挂
const first = invokePredict(VALID_REQUEST);
await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(1));
- // 同一 session 发起 turnGen=1 的新预测,应替换旧条目并允许通过。
- // 旧请求结果终将被 renderer 端 turnGen 校验丢弃,不再阻塞新轮。
+ // 同一 session 发起 updatedAt=2 的新预测(模拟新轮次更新了 session.updatedAt),
+ // 应替换旧条目并允许通过。旧请求结果终将被 renderer 端 turnGen 校验丢弃,不再阻塞新轮。
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 2 };
let resolveSecond!: (value: string) => void;
h.predict.mockImplementationOnce(
() => new Promise<string>((resolve) => { resolveSecond = resolve; }),
@@ -424,18 +425,19 @@ describe('maker:predict-prompt — 多窗口去重', () => {
await expect(first).resolves.toEqual({ prompt: '预测结果A' });
});
- it('同一 session 更低 turnGen 的请求也允许通过(renderer 端 turnGen 校验兜底)', async () => {
+ it('同一 session 不同 updatedAt 的请求也允许通过(renderer 端 turnGen 校验兜底)', async () => {
let resolveFirst!: (value: string) => void;
h.predict.mockImplementationOnce(
() => new Promise<string>((resolve) => { resolveFirst = resolve; }),
);
- // 窗口 A 发起 turnGen=5 的预测并暂挂
+ // 窗口 A 发起 updatedAt=1 的预测并暂挂
const first = invokePredict({ ...VALID_REQUEST, turnGen: 5 });
await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(1));
- // 窗口 B 发起 turnGen=3 的预测(因挂载/会话切换历史不同,turnGen 更低)。
- // 不同 turnGen 允许通过,入口不比对大小;renderer 端 turnGen 校验兜底。
+ // 窗口 B 发起 updatedAt=2 的预测(新轮次,updatedAt 已更新)。
+ // 不同 updatedAt 允许通过,入口不比对大小;renderer 端 turnGen 校验兜底。
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 2 };
let resolveSecond!: (value: string) => void;
h.predict.mockImplementationOnce(
() => new Promise<string>((resolve) => { resolveSecond = resolve; }),
diff --git a/apps/desktop/src/main/maker-ipc/title.ts b/apps/desktop/src/main/maker-ipc/title.ts
index 5ce106f464..30a0491650 100644
--- a/apps/desktop/src/main/maker-ipc/title.ts
+++ b/apps/desktop/src/main/maker-ipc/title.ts
@@ -346,17 +346,18 @@ const PREDICTION_RECENT_MESSAGE_LIMIT = 6;
/** 预测请求:仅 sessionId + agentKind + turnGen。素材(messages / workingDir)一律由 main 从 DB 读取,
* 不信任 renderer 上报内容——受信 renderer 或 stale UI 可能携带其它会话转写 / 伪造 workdir,
- * 把非本会话内容送到本地 provider 触发付费调用。turnGen 用于主进程级去重:同一 session
- * 新 turn 的预测(更高 turnGen)可以替换旧 turn 的进行中请求,避免旧请求阻塞新轮预测。 */
+ * 把非本会话内容送到本地 provider 触发付费调用。turnGen 仅用于 renderer 端结果校验
+ * (丢弃旧轮过期结果),主进程级去重改用 DB 的 session.updatedAt(跨窗口一致)。 */
interface PredictPromptRequest {
sessionId: string;
agentKind: AgentKind;
turnGen: number;
}
-/** 主进程级去重:同一 session 同一 turnGen 同时只能有一笔预测在途,
- * 避免多窗口重复付费调用。与 renderer 侧 _predictingSessions 同模式:
- * Map<sessionId, turnGen>——新 turn 的更高 turnGen 会替换旧条目,不阻塞新轮预测。 */
+/** 主进程级去重:同一 session 同一 updatedAt 同时只能有一笔预测在途,
+ * 避免多窗口重复付费调用。使用 DB session.updatedAt 而非 renderer 上报的 turnGen:
+ * 同一真实轮次在不同窗口的局部 turnGen 可能不同,但 DB updatedAt 跨窗口一致。
+ * Map<sessionId, updatedAt>——新 turn 的不同 updatedAt 会替换旧条目,不阻塞新轮预测。 */
const _predictingPromptSessions = new Map<string, number>();
function parsePredictPromptRequest(raw: unknown): PredictPromptRequest {
@@ -478,22 +479,22 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}
return { prompt: null };
}
// 多窗口去重:同一 session 同时只能有一笔预测在途,避免 openSessionInNewWindow
- // 等多窗口场景下重复触发付费 provider 调用。同 turnGen 的请求是真重复,拒绝;
- // 不同 turnGen 说明新轮已开始,旧轮结果终将被 renderer 的 turnGen 校验丢弃,
+ // 等多窗口场景下重复触发付费 provider 调用。同 updatedAt 的请求是真重复(拒绝);
+ // 不同 updatedAt 说明新轮已开始,旧轮结果终将被 renderer 的 turnGen 校验丢弃,
// 替换旧条目放行新请求,避免旧轮吞掉新轮预测导致当前轮永久没有推荐词。
- const existingTurnGen = _predictingPromptSessions.get(sessionId);
- if (existingTurnGen != null) {
- if (existingTurnGen === turnGen) {
+ if (_predictingPromptSessions.has(sessionId)) {
+ const existingUpdatedAt = _predictingPromptSessions.get(sessionId);
+ if (existingUpdatedAt === sessionRow.updatedAt) {
return { prompt: null };
}
// 旧轮预测仍在途但新轮已开始,旧结果注定被 renderer 丢弃,放行新请求。
log.debug('predict-prompt replacing stale in-flight prediction', {
sessionId,
- oldTurnGen: existingTurnGen,
- newTurnGen: turnGen,
+ oldUpdatedAt: existingUpdatedAt,
+ newUpdatedAt: sessionRow.updatedAt,
});
}
- _predictingPromptSessions.set(sessionId, turnGen);
+ _predictingPromptSessions.set(sessionId, sessionRow.updatedAt);
try {
// 素材只从 DB 读取,不信任 renderer 上报的 messages / workingDir:受信 renderer 或
// stale UI 可能携带其它会话转写或伪造 workdir,把非本会话内容送到本地 provider 触发
@@ -555,8 +556,8 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}
};
} finally {
// 同 session 的预测在上一笔在途时会被拒绝,正常情况下条目始终为请求时刻的
- // turnGen。此处保留相等性校验作为防御性编程。
- if (_predictingPromptSessions.get(sessionId) === turnGen) {
+ // updatedAt。此处保留相等性校验作为防御性编程。
+ if (_predictingPromptSessions.get(sessionId) === sessionRow.updatedAt) {
_predictingPromptSessions.delete(sessionId);
}
}
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 8fd21094a4..a7c99d9d04 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -361,9 +361,11 @@ const TOOLBAR_COMPACT_MAX_WIDTH = 448;
// 预测去重:同一 session 在多个窗口(openSessionInNewWindow)打开时,每个 ChatInput
// 实例都会独立检测到 turn 结束并触发 predictNextPrompt,导致重复的 provider 调用。
-// 模块级 Map<sessionId, turnGen> 跟踪正在进行的预测,确保同一 session 同一 turnGen
-// 同时只有一次预测调用。当旧 turn 的预测仍在途时新 turn 触发预测,新 turn 的
-// turnGen 不同,会替换旧条目并允许新预测通过,避免旧请求吞掉新轮预测。
+// 模块级 Map<sessionId, turnGen> 跟踪正在进行的预测(renderer 端辅助跟踪),
+// 主要用于 cleanup 时防止误删其他窗口的条目。主进程级去重(main 侧 title.ts)
+// 使用 DB session.updatedAt 作为跨窗口一致的去重键,才是真正的付费调用防线。
+// 当旧 turn 的预测仍在途时新 turn 触发预测,新 turn 的 turnGen 不同,
+// 会替换旧条目并允许新预测通过(main 侧也通过 updatedAt 变化放行)。
const _predictingSessions = new Map<string, number>();
function isVoiceInputIdleLike(state: VoiceInputState): boolean {