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): 将 Stop 标记从组件本地 ref 提升为 session 级 store 字段

Shinku8e308ff030d0
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 6a648f6d20..cd4b55ab87 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -1184,10 +1184,12 @@ export function ChatInput({
if (wasRunning && !showStopButton && recommendationEnabled && sessionId && !deviceLinkDeviceId && !remoteHostId) {
// 后台 wake 型任务(local_agent / local_workflow)仍在运行时,主 turn 报告
// 用户点击 Stop 或 turn 以错误/中止结束时,不应触发预测。
- if (wasTurnStoppedByUserRef.current) return;
- // turn 以终端错误结束时,不应触发预测:错误上下文可能包含不完整/损坏的对话,
- // 避免在错误状态下发起付费 provider 调用。
- if (makerChatStore.getSnapshot(sessionId)?.error) return;
+ // turnStoppedByUser 是 session 级 store 字段(stopSession 置位、新 turn 复位),
+ // 确保同一 session 在多窗口打开时,任一窗口的 Stop 都能阻止其他窗口触发预测。
+ if (makerChatStore.getSnapshot(sessionId)?.turnStoppedByUser) return;
+ // turn 以终端错误结束时,不应触发预测:错误上下文可能包含不完整/损坏的对话,
+ // 避免在错误状态下发起付费 provider 调用。
+ if (makerChatStore.getSnapshot(sessionId)?.error) return;
// stopped 但会话仍在工作 —— 跳过预测,避免用不完整上下文发起付费调用。
// hasBackgroundAgentWork 已在 _isSessionBusy 里统一折算,这里单独补门禁。
if (makerChatStore.hasBackgroundAgentWork(sessionId)) return;
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 3e1b71d063..9cba0374ba 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -2500,6 +2500,13 @@ export interface SessionChatState {
* reloadMessages。
*/
pendingTaskWake: boolean;
+ /**
+ * 用户主动 Stop 标记:会话级(非组件级),确保同一 session 在多窗口打开时
+ * 任一窗口的 Stop 都能阻止其他窗口触发预测等后续行为。
+ * 置位:stopSession(用户主动 Stop 时)。
+ * 清除:新 turn 启动(isRunning false→true)时复位。
+ */
+ turnStoppedByUser: boolean;
/**
* agent-meta: 上一条 SDK assistant message 的 cc 元信息——mid-turn 抢救
* assistant 累积流(tool_use / ask_user / plan_review 切流时把累积文本落
@@ -2548,6 +2555,7 @@ export type SessionChatLightState = Pick<
| 'planModeEnabled'
| 'agentSwitchIntent'
| 'pendingTaskWake'
+ | 'turnStoppedByUser'
>;
function createInitialState(): SessionChatState {
@@ -2613,6 +2621,7 @@ function createInitialState(): SessionChatState {
planModeRev: 0,
lastStopWasSideTask: false,
pendingTaskWake: false,
+ turnStoppedByUser: false,
lastAgentMeta: null,
};
}
@@ -2680,6 +2689,7 @@ export const EMPTY_SESSION_STATE: SessionChatState = Object.freeze({
planModeRev: 0,
lastStopWasSideTask: false,
pendingTaskWake: false,
+ turnStoppedByUser: false,
lastAgentMeta: null,
}) as SessionChatState;
@@ -5509,6 +5519,7 @@ function forceFinalizeOnSessionClosed(state: SessionChatState): SessionChatState
// 清零,否则 running 快照(折算了后台任务)会让 spinner 永久转下去。
taskUpdates: stoppedTasks,
pendingTaskWake: false,
+ turnStoppedByUser: false,
agentStatus: {
...finalized.agentStatus,
isRunning: false,
@@ -5597,6 +5608,7 @@ function handleStatusUpdate(
// isRunning:true)是正常路径;wake turn 秒挂(只来 error + Done)也在这里
// 收口,防止桥接标记漏网永久撑住 running 快照。skipTurnReset 已提前 return。
pendingTaskWake: false,
+ turnStoppedByUser: false,
agentStatus: {
status: update.status,
tokenUsage: tu,
@@ -7695,7 +7707,8 @@ function lightStateEquals(a: SessionChatLightState, b: SessionChatLightState): b
a.queueExpanded === b.queueExpanded &&
a.fastMode === b.fastMode &&
a.planModeEnabled === b.planModeEnabled &&
- a.pendingTaskWake === b.pendingTaskWake
+ a.pendingTaskWake === b.pendingTaskWake &&
+ a.turnStoppedByUser === b.turnStoppedByUser
);
}
@@ -9523,6 +9536,7 @@ function reloadMessages(sessionId: string, opts?: { allowCacheHydrate?: boolean
messages: optimisticMessages,
taskUpdates: new Map(),
pendingTaskWake: false,
+ turnStoppedByUser: false,
historyLoaded: false,
hasMoreMessages: false,
oldestMessageId: null,
@@ -12084,6 +12098,7 @@ function stopSession(
// 后续 task_progress 会把条目翻回 running,状态自愈。
taskUpdates: stopRunningAgentTasks(s.taskUpdates, 'wake'),
pendingTaskWake: false,
+ turnStoppedByUser: true,
agentStatus: {
status: 'Idle',
// Preserve all token/cost values — ring keeps showing last known context capacity
@@ -12390,6 +12405,7 @@ async function clearSessionAfterGuardImpl(sessionId: string, clearedAt: string):
),
taskUpdates: new Map(),
pendingTaskWake: false,
+ turnStoppedByUser: false,
streamingClientId: null,
streamingText: '',
isStreaming: false,