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): 恢复启动失败后的完成提示

Dash3c2a777b1ded
diff --git a/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts b/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts
index b933e67823..5f803d2cb1 100644
--- a/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts
+++ b/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts
@@ -48,6 +48,7 @@ vi.mock('@/lib/sessionAttentionStore', () => ({
vi.mock('@/lib/sessionStartingStore', () => ({
getStartingSessionIds: () => startingMock.sessionIds,
+ useStartingSessionIds: () => startingMock.sessionIds,
}));
function status(isRunning: boolean, hasError = false, sideTask?: boolean): SessionStatusInfo {
@@ -75,7 +76,7 @@ describe('useSessionRunningStatus silenced completion handling', () => {
storeMock.listeners.clear();
storeMock.terminalErrorSessions.clear();
storeMock.sideTaskStopSessions.clear();
- startingMock.sessionIds.clear();
+ startingMock.sessionIds = new Set();
resetSilencedSessionDoneStoreForTests();
vi.clearAllMocks();
vi.restoreAllMocks();
@@ -356,6 +357,45 @@ describe('useSessionRunningStatus silenced completion handling', () => {
expect(onSessionDone).toHaveBeenCalledWith('session-starting');
});
+ it('restores deferred done attention when starting clears without a new run', async () => {
+ vi.useFakeTimers();
+ const onSessionDone = vi.fn();
+ const { rerender } = renderHook(() => useSessionRunningStatus(undefined, { onSessionDone }));
+
+ await emitSnapshot(new Map([['session-failed-start', status(true)]]));
+ await emitSnapshot(new Map([['session-failed-start', status(false)]]));
+ startingMock.sessionIds.add('session-failed-start');
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(500);
+ });
+ expect(vi.mocked(addSessionAttention)).not.toHaveBeenCalledWith('session-failed-start', 'done');
+
+ startingMock.sessionIds = new Set();
+ rerender();
+
+ expect(vi.mocked(addSessionAttention)).toHaveBeenCalledWith('session-failed-start', 'done');
+ expect(onSessionDone).toHaveBeenCalledTimes(1);
+ });
+
+ it('drops deferred done attention when starting becomes a real run', async () => {
+ vi.useFakeTimers();
+ const onSessionDone = vi.fn();
+ renderHook(() => useSessionRunningStatus(undefined, { onSessionDone }));
+
+ await emitSnapshot(new Map([['session-started', status(true)]]));
+ await emitSnapshot(new Map([['session-started', status(false)]]));
+ startingMock.sessionIds.add('session-started');
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(500);
+ });
+
+ startingMock.sessionIds = new Set();
+ await emitSnapshot(new Map([['session-started', status(true)]]));
+
+ expect(vi.mocked(addSessionAttention)).not.toHaveBeenCalledWith('session-started', 'done');
+ expect(onSessionDone).toHaveBeenCalledTimes(1);
+ });
+
it('coalesces multiple queue-drain transitions into a single final notification', async () => {
// 排队 3 条消息:A → done → B → done → C → done。前两次 done 都在 debounce
// 窗口内被下一次 turn 起始取消,只有 C 结束(队列空)最终 fire 一次通知。
diff --git a/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts b/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts
index f73d0f9059..b74a017720 100644
--- a/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts
+++ b/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts
@@ -53,7 +53,7 @@ import {
isSessionTerminalNotificationOwnedByScheduler,
isSessionDoneSilenced,
} from '@/lib/silencedSessionDoneStore';
-import { getStartingSessionIds } from '@/lib/sessionStartingStore';
+import { getStartingSessionIds, useStartingSessionIds } from '@/lib/sessionStartingStore';
// Codex maker 化后, codex session 也走 makerChatStore;
// 不再需要双 store 合并 —— 直接订阅 makerChatStore 即可。
@@ -65,6 +65,15 @@ import { getStartingSessionIds } from '@/lib/sessionStartingStore';
*/
const QUEUE_DEBOUNCE_MS = 500;
+function hasPendingInteraction(info: SessionStatusInfo | undefined): boolean {
+ return !!info && (
+ info.hasPendingAskUser ||
+ info.hasPendingPermission ||
+ info.hasPendingPlanReview ||
+ info.hasPendingPluginSetup
+ );
+}
+
interface UseSessionRunningStatusOptions {
/**
* Fired when a session transitions running → done. Filters out errored
@@ -105,6 +114,7 @@ export function useSessionRunningStatus(
makerChatStore.getRunningSnapshot,
makerChatStore.getRunningSnapshot,
);
+ const startingSessionIds = useStartingSessionIds();
// Latest callbacks — kept in refs so the transition effect doesn't
// re-run when the caller passes inline lambdas each render.
@@ -138,6 +148,11 @@ export function useSessionRunningStatus(
new Map<string, ReturnType<typeof setTimeout>>(),
);
+ // debounce 到期时下一轮仍处于 starting,先暂缓上一轮 done 角标。starting
+ // 若随后失败 / 暂停 / TTL 清除且没有权威运行态,再恢复该角标;成功进入
+ // running 则丢弃。系统通知 callback 已在 debounce 到期时触发,这里只补角标。
+ const pendingStartingDoneAttentionRef = useRef(new Set<string>());
+
const notifications = useSessionAttentionSnapshot();
// Detect transitions that should add or remove notifications.
@@ -145,6 +160,22 @@ export function useSessionRunningStatus(
const currentRunningSet = deriveRunningSet(statusMap);
const prevRunning = prevRunningRef.current;
+ // starting 是乐观态,可能以失败 / 暂停结束而从未进入 running。若 debounce
+ // 期间因此抑制了上一轮 done,starting 清除后必须按当前权威状态收口。
+ for (const sessionId of pendingStartingDoneAttentionRef.current) {
+ const cur = statusMap.get(sessionId);
+ const isActive = sessionId === activeSessionId;
+ const isRunning = cur?.isRunning === true;
+ const stillPending = hasPendingInteraction(cur);
+ if (isActive || isRunning || stillPending) {
+ pendingStartingDoneAttentionRef.current.delete(sessionId);
+ continue;
+ }
+ if (startingSessionIds.has(sessionId)) continue;
+ pendingStartingDoneAttentionRef.current.delete(sessionId);
+ addSessionAttention(sessionId, 'done');
+ }
+
// --- 1. Detect new turn starts ---
for (const sessionId of currentRunningSet) {
if (!prevRunning.has(sessionId)) {
@@ -239,14 +270,13 @@ export function useSessionRunningStatus(
const isActive = sessionId === activeSessionIdRef.current;
const isRunning = cur?.isRunning === true;
const isStarting = getStartingSessionIds().has(sessionId);
- const stillPending =
- !!cur &&
- (cur.hasPendingAskUser ||
- cur.hasPendingPermission ||
- cur.hasPendingPlanReview ||
- cur.hasPendingPluginSetup);
- if (!isActive && !isRunning && !isStarting && !stillPending) {
- addSessionAttention(sessionId, 'done');
+ const stillPending = hasPendingInteraction(cur);
+ if (!isActive && !isRunning && !stillPending) {
+ if (isStarting) {
+ pendingStartingDoneAttentionRef.current.add(sessionId);
+ } else {
+ addSessionAttention(sessionId, 'done');
+ }
}
if (!notificationOwnedByScheduler) onSessionDoneRef.current?.(sessionId);
}, QUEUE_DEBOUNCE_MS);
@@ -257,12 +287,7 @@ export function useSessionRunningStatus(
// --- 3. Detect pending ask-user / permission / plan-review / plugin-setup ---
const currentPendingSet = new Set<string>();
for (const [id, info] of statusMap) {
- if (
- info.hasPendingAskUser ||
- info.hasPendingPermission ||
- info.hasPendingPlanReview ||
- info.hasPendingPluginSetup
- ) {
+ if (hasPendingInteraction(info)) {
currentPendingSet.add(id);
const isActive = id === activeSessionId;
const wasAlreadyPending = prevPendingRef.current.has(id);
@@ -310,7 +335,7 @@ export function useSessionRunningStatus(
prevRunningRef.current = new Set(currentRunningSet);
prevPendingRef.current = currentPendingSet;
- }, [statusMap, activeSessionId]);
+ }, [statusMap, activeSessionId, startingSessionIds]);
// Unmount cleanup:清掉所有还没 fire 的 done debounce 定时器,避免 hook 卸载后
// 定时器仍触发 addSessionAttention / onSessionDone(referring stale refs 后果小,
@@ -320,6 +345,7 @@ export function useSessionRunningStatus(
return () => {
for (const t of timers.values()) clearTimeout(t);
timers.clear();
+ pendingStartingDoneAttentionRef.current.clear();
};
}, []);