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(promptPrediction): 修复旧轮预测吞掉新轮推荐 + 目录变化时清除推荐

Shinkueeeb294a56aa
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 f44aa302f6..0fb3c17ca6 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
@@ -374,7 +374,7 @@ describe('maker:predict-prompt — 多窗口去重', () => {
await expect(first).resolves.toEqual({ prompt: '预测结果' });
});
- it('同一 session 新 turnGen 的请求被拒绝(当已有在途请求时,避免重复付费)', async () => {
+ it('同一 session 新 turnGen 的请求替换旧请求(旧轮结果终将被 renderer turnGen 校验丢弃)', async () => {
let resolveFirst!: (value: string) => void;
h.predict.mockImplementationOnce(
() => new Promise<string>((resolve) => { resolveFirst = resolve; }),
@@ -384,21 +384,25 @@ describe('maker:predict-prompt — 多窗口去重', () => {
const first = invokePredict(VALID_REQUEST);
await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(1));
- // 同一 session 发起 turnGen=1 的新预测,应被拒绝(已有在途请求)
+ // 同一 session 发起 turnGen=1 的新预测,应替换旧条目并允许通过。
+ // 旧请求结果终将被 renderer 端 turnGen 校验丢弃,不再阻塞新轮。
+ let resolveSecond!: (value: string) => void;
+ h.predict.mockImplementationOnce(
+ () => new Promise<string>((resolve) => { resolveSecond = resolve; }),
+ );
const second = invokePredict({
...VALID_REQUEST,
turnGen: 1,
});
- await expect(second).resolves.toEqual({ prompt: null });
+ await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(2));
- // 旧预测完成,去重条目应被清除
+ // 旧预测完成,去重条目已被新请求替换
resolveFirst('旧轮预测');
await expect(first).resolves.toEqual({ prompt: '旧轮预测' });
- // 旧预测完成后,新请求可以通过
- h.predict.mockImplementationOnce(async () => '新预测');
- const third = invokePredict({ ...VALID_REQUEST, turnGen: 2 });
- await expect(third).resolves.toEqual({ prompt: '新预测' });
+ // 新预测完成
+ resolveSecond('新轮预测');
+ await expect(second).resolves.toEqual({ prompt: '新轮预测' });
});
it('不同 session 可以并发预测', async () => {
@@ -420,7 +424,7 @@ describe('maker:predict-prompt — 多窗口去重', () => {
await expect(first).resolves.toEqual({ prompt: '预测结果A' });
});
- it('同一 session 更低 turnGen 的请求被拒绝(跨窗口旧窗口场景)', async () => {
+ it('同一 session 更低 turnGen 的请求也允许通过(renderer 端 turnGen 校验兜底)', async () => {
let resolveFirst!: (value: string) => void;
h.predict.mockImplementationOnce(
() => new Promise<string>((resolve) => { resolveFirst = resolve; }),
@@ -430,12 +434,21 @@ describe('maker:predict-prompt — 多窗口去重', () => {
const first = invokePredict({ ...VALID_REQUEST, turnGen: 5 });
await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(1));
- // 窗口 B 发起 turnGen=3 的预测(因挂载/会话切换历史不同,turnGen 更低),应被拒绝
+ // 窗口 B 发起 turnGen=3 的预测(因挂载/会话切换历史不同,turnGen 更低)。
+ // 不同 turnGen 允许通过,入口不比对大小;renderer 端 turnGen 校验兜底。
+ let resolveSecond!: (value: string) => void;
+ h.predict.mockImplementationOnce(
+ () => new Promise<string>((resolve) => { resolveSecond = resolve; }),
+ );
const second = invokePredict({ ...VALID_REQUEST, turnGen: 3 });
- await expect(second).resolves.toEqual({ prompt: null });
+ await vi.waitFor(() => expect(h.predict).toHaveBeenCalledTimes(2));
// 窗口 A 的预测完成
- resolveFirst('预测结果');
- await expect(first).resolves.toEqual({ prompt: '预测结果' });
+ resolveFirst('预测结果A');
+ await expect(first).resolves.toEqual({ prompt: '预测结果A' });
+
+ // 窗口 B 的预测完成
+ resolveSecond('预测结果B');
+ await expect(second).resolves.toEqual({ prompt: '预测结果B' });
});
});
\ No newline at end of file
diff --git a/apps/desktop/src/main/maker-ipc/title.ts b/apps/desktop/src/main/maker-ipc/title.ts
index 672ae39507..5ce106f464 100644
--- a/apps/desktop/src/main/maker-ipc/title.ts
+++ b/apps/desktop/src/main/maker-ipc/title.ts
@@ -478,14 +478,20 @@ export function registerMakerTitleIpc(options: RegisterMakerTitleIpcOptions = {}
return { prompt: null };
}
// 多窗口去重:同一 session 同时只能有一笔预测在途,避免 openSessionInNewWindow
- // 等多窗口场景下重复触发付费 provider 调用。仅按 sessionId 去重:当已有同 session
- // 的预测在途时拒绝所有新请求,不按 turnGen 比较替换。turnGen 是各窗口独立维护的
- // 局部计数器,不存在跨窗口全局序:高 turnGen 请求替换 Map 条目后会留下旧请求仍在
- // 途但失去去重保护,导致同一轮并行产生两次付费调用;后到请求先结束时还会删除条目,
- // 使仍在途的旧请求完全失去去重保护。
+ // 等多窗口场景下重复触发付费 provider 调用。同 turnGen 的请求是真重复,拒绝;
+ // 不同 turnGen 说明新轮已开始,旧轮结果终将被 renderer 的 turnGen 校验丢弃,
+ // 替换旧条目放行新请求,避免旧轮吞掉新轮预测导致当前轮永久没有推荐词。
const existingTurnGen = _predictingPromptSessions.get(sessionId);
if (existingTurnGen != null) {
- return { prompt: null };
+ if (existingTurnGen === turnGen) {
+ return { prompt: null };
+ }
+ // 旧轮预测仍在途但新轮已开始,旧结果注定被 renderer 丢弃,放行新请求。
+ log.debug('predict-prompt replacing stale in-flight prediction', {
+ sessionId,
+ oldTurnGen: existingTurnGen,
+ newTurnGen: turnGen,
+ });
}
_predictingPromptSessions.set(sessionId, turnGen);
try {
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 2fd8157db2..8fd21094a4 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -1228,9 +1228,10 @@ export function ChatInput({
role: m.role,
content: m.content,
}));
- // 捕获请求时刻的 sessionId 与 turnGen 快照,落地前校验是否仍匹配。
+ // 捕获请求时刻的 sessionId、turnGen 与 workingDir 快照,落地前校验是否仍匹配。
const requestSessionId = sessionId;
const requestTurnGen = turnGenRef.current;
+ const requestWorkingDir = workingDir;
// 去重:同一 session 同一 turnGen 同时只有一次预测调用,避免重复 provider 调用与费用。
// 当旧 turn 预测仍在途时新 turn 触发预测,新 turnGen 不同 → 替换旧条目,允许新预测通过。
const existingTurnGen = _predictingSessions.get(requestSessionId);
@@ -1259,7 +1260,8 @@ export function ChatInput({
composerFullyEmptyRef.current() &&
!showStopButtonRef.current &&
prevSessionIdRef.current === requestSessionId &&
- turnGenRef.current === requestTurnGen
+ turnGenRef.current === requestTurnGen &&
+ workingDirRef.current === requestWorkingDir
) {
showRecommendationRef.current = true;
setRecommendedPrompt(result.prompt);
@@ -1464,6 +1466,13 @@ export function ChatInput({
const [workingDir, setWorkingDir] = useState<string | null>(initialWorkingDir ?? null);
const [internalFolderOpen, setInternalFolderOpen] = useState(false);
+ // 工作目录变化 → 作废在途推荐并清除已显示推荐,避免旧目录上下文诱导错误操作。
+ useEffect(() => {
+ turnGenRef.current += 1;
+ showRecommendationRef.current = false;
+ setRecommendedPrompt(null);
+ }, [workingDir]);
+
// Ref bridge for Tiptap handlePaste(粘贴管线):editorProps 闭包只建一次,
// 读不到最新 state / props / t——粘贴时的 workdir(路径识别范围)、会话来源
// (stat 路由)、当前语言(pasted chip 文案)全部经 ref 取现值。