Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 去重键改用 userSendAt + Tab 填入前检查推荐可见性
Shinkua2e12c1bd8cc
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.tsindex c1ff2b4f76..c8fceccd1c 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, updatedAt: 1 } as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null; updatedAt?: number } | undefined,+ sessionRow: { remoteHostId: null, agentKind: null, userSendAt: 1 } as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null; updatedAt?: number; userSendAt?: number } | undefined,/** 模拟 DB 第二次读取(排空落盘队列之后)返回的 session row;用于「drain 期间被删除」竞态测试。 */- sessionRowAfterDrain: undefined as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null; updatedAt?: number } | undefined,+ sessionRowAfterDrain: undefined as { remoteHostId: string | null; source?: string | null; agentKind: string | null; workingDir?: string | null; status?: string | null; updatedAt?: number; userSendAt?: 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', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 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', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'pi', workingDir: '/db/workdir', userSendAt: 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', updatedAt: 1 };+ h.sessionRow = { remoteHostId: 'ssh-host-1', agentKind: 'cc', userSendAt: 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', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 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', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'codex', userSendAt: 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', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', status: 'deleted', userSendAt: 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', updatedAt: 1 };- h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'cc', status: 'deleted', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 1 };+ h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'cc', status: 'deleted', userSendAt: 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', updatedAt: 1 };- h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'codex', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 1 };+ h.sessionRowAfterDrain = { remoteHostId: null, agentKind: 'codex', userSendAt: 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 同一 turn(DB updatedAt 不变)并发调用时第二个请求直接返回 null,避免重复付费', async () => {+ it('同一 session 同一 turn(DB userSendAt 不变)并发调用时第二个请求直接返回 null,避免重复付费', async () => {// 让第一次预测暂挂,模拟并发let resolveFirst!: (value: string) => void;h.predict.mockImplementationOnce(@@ -374,7 +374,7 @@ describe('maker:predict-prompt — 多窗口去重', () => {await expect(first).resolves.toEqual({ prompt: '预测结果' });});- it('同一 session 新 turn(DB updatedAt 变化)的预测可以替换旧条目的进行中请求', async () => {+ it('同一 session 新 turn(DB userSendAt 变化)的预测可以替换旧条目的进行中请求', async () => {let resolveFirst!: (value: string) => void;let resolveSecond!: (value: string) => void;h.predict@@ -385,22 +385,22 @@ describe('maker:predict-prompt — 多窗口去重', () => {() => new Promise<string>((resolve) => { resolveSecond = resolve; }),);- // 发起 updatedAt=1 的预测并暂挂- h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };+ // 发起 userSendAt=1 的预测并暂挂+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 1 };const first = invokePredict(VALID_REQUEST);await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(1));- // 同一 session DB updatedAt 变为 2(新 turn),应能通过(替换旧条目)- h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 2 };+ // 同一 session DB userSendAt 变为 2(新 turn),应能通过(替换旧条目)+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 2 };const second = invokePredict(VALID_REQUEST);await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(2));- // 旧预测完成,因 updatedAt 不匹配,不应删除去重条目+ // 旧预测完成,因 userSendAt 不匹配,不应删除去重条目resolveFirst('旧轮预测');await expect(first).resolves.toEqual({ prompt: '旧轮预测' });- // 新预测仍在途,去重条目应仍为 updatedAt=2- h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 2 };+ // 新预测仍在途,去重条目应仍为 userSendAt=2+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 2 };const third = invokePredict(VALID_REQUEST);await expect(third).resolves.toEqual({ prompt: null });@@ -428,20 +428,20 @@ describe('maker:predict-prompt — 多窗口去重', () => {await expect(first).resolves.toEqual({ prompt: '预测结果A' });});- it('同一 session DB updatedAt 不变时拒绝重复请求,避免跨窗口重复付费', async () => {+ it('同一 session DB userSendAt 不变时拒绝重复请求,避免跨窗口重复付费', async () => {let resolveFirst!: (value: string) => void;h.predict.mockImplementationOnce(() => new Promise<string>((resolve) => { resolveFirst = resolve; }),);- // 窗口 A 发起预测(DB updatedAt=1)并暂挂- h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };+ // 窗口 A 发起预测(DB userSendAt=1)并暂挂+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 1 };const first = invokePredict(VALID_REQUEST);await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(1));- // 窗口 B 同时发起预测,DB updatedAt 仍为 1(同一 turn),应被拒绝+ // 窗口 B 同时发起预测,DB userSendAt 仍为 1(同一 turn),应被拒绝// (无论窗口 B 的局部 turnGen 是多少,DB 记录才是权威去重依据)- h.sessionRow = { remoteHostId: null, agentKind: 'cc', updatedAt: 1 };+ h.sessionRow = { remoteHostId: null, agentKind: 'cc', userSendAt: 1 };const second = invokePredict({ ...VALID_REQUEST, turnGen: 5 });await expect(second).resolves.toEqual({ prompt: null });diff --git a/apps/desktop/src/main/maker-ipc/title.ts b/apps/desktop/src/main/maker-ipc/title.tsindex a4dc2ce00d..bc6f53400a 100644--- a/apps/desktop/src/main/maker-ipc/title.ts+++ b/apps/desktop/src/main/maker-ipc/title.ts@@ -347,7 +347,7 @@ const PREDICTION_RECENT_MESSAGE_LIMIT = 6;/** 预测请求:仅 sessionId + agentKind + turnGen。素材(messages / workingDir)一律由 main 从 DB 读取,* 不信任 renderer 上报内容——受信 renderer 或 stale UI 可能携带其它会话转写 / 伪造 workdir,* 把非本会话内容送到本地 provider 触发付费调用。turnGen 用于 IPC 参数校验与 renderer 侧去重;- * 主进程级去重使用 DB 的权威 updatedAt(见 _predictingPromptSessions)。 */+ * 主进程级去重使用 DB 的权威 userSendAt(见 _predictingPromptSessions)。 */interface PredictPromptRequest {sessionId: string;agentKind: AgentKind;@@ -356,9 +356,12 @@ interface PredictPromptRequest {/** 主进程级去重:同一 session 同时只能有一笔预测在途,* 避免多窗口重复付费调用。与 renderer 侧 _predictingSessions 同模式:- * Map<sessionId, updatedAt(DB 权威值)>——新 turn 的 DB updatedAt 变化时替换旧条目。- * 使用 DB 的权威 updatedAt 而非 renderer 的局部 turnGen:同一 session 的- * 任意窗口都读到相同的 DB 记录,避免跨窗口代次顺序失真。 */+ * Map<sessionId, userSendAt(DB 权威值)>——新 turn 的 DB userSendAt 变化时替换旧条目。+ * 使用 DB 的权威 userSendAt 而非 renderer 的局部 turnGen:同一 session 的+ * 任意窗口都读到相同的 DB 记录,避免跨窗口代次顺序失真。+ * 选 userSendAt 而非 updatedAt:updatedAt 会因非 turn 更新(agent 切换、标题变更等)+ * 而改变,导致去重键失真——非 turn 更新允许重复请求,或新 turn 被当作重复请求拒绝。+ * userSendAt 仅由「用户按下发送」bump,不受非 turn 更新干扰,是更可靠的轮次标识。 */const _predictingPromptSessions = new Map<string, number>();function parsePredictPromptRequest(raw: unknown): PredictPromptRequest {@@ -462,7 +465,7 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}// 对其做预测是浪费付费调用。也拒绝 soft-deleted session:删除态会话的消息仍保留在 DB,// 但已从用户会话列表移除,对其做预测会外发已删除转写并产生付费调用。const [sessionRow] = await getDbClient()- .drizzle.select({ remoteHostId: sessions.remoteHostId, source: sessions.source, agentKind: sessions.agentKind, workingDir: sessions.workingDir, status: sessions.status, updatedAt: sessions.updatedAt })+ .drizzle.select({ remoteHostId: sessions.remoteHostId, source: sessions.source, agentKind: sessions.agentKind, workingDir: sessions.workingDir, status: sessions.status, updatedAt: sessions.updatedAt, userSendAt: sessions.userSendAt }).from(sessions).where(eq(sessions.id, sessionId));if (!sessionRow || sessionRow.remoteHostId || sessionRow.source === 'review' || sessionRow.status === 'deleted') {@@ -480,15 +483,22 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}return { prompt: null };}// 多窗口去重:同一 session 同时只能有一笔预测在途,避免 openSessionInNewWindow- // 等多窗口场景下重复触发付费 provider 调用。使用 DB 的权威 updatedAt 而非+ // 等多窗口场景下重复触发付费 provider 调用。使用 DB 的权威 userSendAt 而非// renderer 的局部 turnGen 作为去重键:同一 session 的任意窗口都读到相同的 DB- // 记录,避免跨窗口代次顺序失真。若 DB 状态未变(同一轮),拒绝重复请求;若 DB- // 状态已变(新轮),允许新请求替换旧条目。- const existingUpdatedAt = _predictingPromptSessions.get(sessionId);- if (existingUpdatedAt != null && sessionRow.updatedAt === existingUpdatedAt) {+ // 记录,避免跨窗口代次顺序失真。若 DB 的 userSendAt 未变(同一轮),拒绝重复请求;+ // 若 userSendAt 已变(新轮),允许新请求替换旧条目。+ // 选 userSendAt 而非 updatedAt:updatedAt 会因非 turn 更新(agent 切换、标题变更等)+ // 而改变,导致去重键失真——非 turn 更新允许重复请求,或新 turn 被当作重复请求拒绝。+ // userSendAt 仅由「用户按下发送」bump,不受非 turn 更新干扰,是更可靠的轮次标识。+ const existingUserSendAt = _predictingPromptSessions.get(sessionId);+ if (existingUserSendAt != null && sessionRow.userSendAt === existingUserSendAt) {return { prompt: null };}- _predictingPromptSessions.set(sessionId, sessionRow.updatedAt);+ // userSendAt 为 NULL 表示从未发过消息(草稿会话),不应触发预测。+ if (sessionRow.userSendAt == null) {+ return { prompt: null };+ }+ _predictingPromptSessions.set(sessionId, sessionRow.userSendAt);try {// 素材只从 DB 读取,不信任 renderer 上报的 messages / workingDir:受信 renderer 或// stale UI 可能携带其它会话转写或伪造 workdir,把非本会话内容送到本地 provider 触发@@ -549,9 +559,9 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}}),};} finally {- // 仅在 Map 条目仍为请求时刻的 updatedAt 时才删除,防止旧轮预测结束时+ // 仅在 Map 条目仍为请求时刻的 userSendAt 时才删除,防止旧轮预测结束时// 清除新轮预测的去重保护(与 renderer 侧 _predictingSessions 同模式)。- if (_predictingPromptSessions.get(sessionId) === sessionRow.updatedAt) {+ if (_predictingPromptSessions.get(sessionId) === sessionRow.userSendAt) {_predictingPromptSessions.delete(sessionId);}}diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsxindex bec525c0b5..b1f97a0e14 100644--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx@@ -2849,6 +2849,21 @@ export function ChatInput({useEffect(() => {editor?.setEditable(!composerMutationLocked);}, [composerMutationLocked, editor]);++ // 当推荐因附件/浏览器评论/语音草稿变为不可见时,清除 ref 防止 Tab 填入隐藏内容。+ // showRecommendationOverlay 的可见判据包含 !hasAttachments / browserComments.length === 0 /+ // !hasVoiceDraftText,但当这些条件变为 false 时只隐藏 overlay,不清除 ref。+ // 此时按 Tab 仍会通过 showRecommendationRef.current 检查并插入不可见的推荐词。+ useEffect(() => {+ if (+ recommendedPromptRef.current &&+ (attachments.length > 0 || browserComments.length > 0 || voiceInput.draftText.trim().length > 0)+ ) {+ showRecommendationRef.current = false;+ setRecommendedPrompt(null);+ }+ }, [attachments.length, browserComments.length, voiceInput.draftText]);+const captureSendFocusForRestore = useComposerSendFocusRestore(editor,composerMutationLocked,