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): preserve completion across failed starts

Dash867e99bd54c8
diff --git a/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts b/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts
index 7afa3152d8..dfe105dfbb 100644
--- a/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts
+++ b/apps/desktop/src/renderer/__tests__/useSessionRunningStatusSilence.test.ts
@@ -11,8 +11,13 @@ import {
scheduleClearSchedulerOwnedRun,
scheduleClearSilencedRun,
} from '@/lib/silencedSessionDoneStore';
+import { markSessionStarting, resetSessionStartingStoreForTests } from '@/lib/sessionStartingStore';
import { useSessionRunningStatus } from '@/hooks/useSessionRunningStatus';
-import { addSessionAttention, clearSessionAttention, getSessionAttentionKind } from '@/lib/sessionAttentionStore';
+import {
+ addSessionAttention,
+ clearSessionAttention,
+ getSessionAttentionKind,
+} from '@/lib/sessionAttentionStore';
const storeMock = vi.hoisted(() => ({
snapshot: new Map<string, SessionStatusInfo>(),
@@ -20,9 +25,6 @@ const storeMock = vi.hoisted(() => ({
terminalErrorSessions: new Set<string>(),
sideTaskStopSessions: new Set<string>(),
}));
-const startingMock = vi.hoisted(() => ({
- sessionIds: new Set<string>(),
-}));
vi.mock('@/lib/makerChatStore', () => ({
makerChatStore: {
@@ -46,10 +48,6 @@ vi.mock('@/lib/sessionAttentionStore', () => ({
useSessionAttentionSnapshot: () => new Set<string>(),
}));
-vi.mock('@/lib/sessionStartingStore', () => ({
- getStartingSessionIds: () => startingMock.sessionIds,
-}));
-
function status(isRunning: boolean, hasError = false, sideTask?: boolean): SessionStatusInfo {
return {
isRunning,
@@ -75,8 +73,8 @@ describe('useSessionRunningStatus silenced completion handling', () => {
storeMock.listeners.clear();
storeMock.terminalErrorSessions.clear();
storeMock.sideTaskStopSessions.clear();
- startingMock.sessionIds.clear();
resetSilencedSessionDoneStoreForTests();
+ resetSessionStartingStoreForTests();
vi.clearAllMocks();
vi.restoreAllMocks();
vi.useRealTimers();
@@ -374,19 +372,35 @@ describe('useSessionRunningStatus silenced completion handling', () => {
expect(onSessionDone).toHaveBeenCalledWith('session-running');
});
- it('does not restore done attention while the next turn is starting', async () => {
+ it('does not overwrite a terminal error that appears during the done debounce', async () => {
+ vi.useFakeTimers();
+ const onSessionDone = vi.fn();
+ renderHook(() => useSessionRunningStatus(undefined, { onSessionDone }));
+
+ await emitSnapshot(new Map([['session-error', status(true)]]));
+ await emitSnapshot(new Map([['session-error', status(false)]]));
+ storeMock.terminalErrorSessions.add('session-error');
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(500);
+ });
+
+ expect(vi.mocked(addSessionAttention)).not.toHaveBeenCalledWith('session-error', 'done');
+ expect(onSessionDone).not.toHaveBeenCalled();
+ });
+
+ it('preserves the prior done attention if the next turn stays in starting', async () => {
vi.useFakeTimers();
const onSessionDone = vi.fn();
renderHook(() => useSessionRunningStatus(undefined, { onSessionDone }));
await emitSnapshot(new Map([['session-starting', status(true)]]));
await emitSnapshot(new Map([['session-starting', status(false)]]));
- startingMock.sessionIds.add('session-starting');
+ markSessionStarting('session-starting');
await act(async () => {
await vi.advanceTimersByTimeAsync(500);
});
- expect(vi.mocked(addSessionAttention)).not.toHaveBeenCalledWith('session-starting', 'done');
+ expect(vi.mocked(addSessionAttention)).toHaveBeenCalledWith('session-starting', 'done');
expect(onSessionDone).toHaveBeenCalledWith('session-starting');
});
@@ -402,18 +416,24 @@ describe('useSessionRunningStatus silenced completion handling', () => {
await emitSnapshot(new Map([['session-1', status(true)]]));
// A done
await emitSnapshot(new Map([['session-1', status(false)]]));
- await act(async () => { await vi.advanceTimersByTimeAsync(50); });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(50);
+ });
// B running
await emitSnapshot(new Map([['session-1', status(true)]]));
// B done
await emitSnapshot(new Map([['session-1', status(false)]]));
- await act(async () => { await vi.advanceTimersByTimeAsync(50); });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(50);
+ });
// C running
await emitSnapshot(new Map([['session-1', status(true)]]));
// C done —— 队列空
await emitSnapshot(new Map([['session-1', status(false)]]));
// 推进过 debounce 窗口
- await act(async () => { await vi.advanceTimersByTimeAsync(600); });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(600);
+ });
expect(onSessionDone).toHaveBeenCalledTimes(1);
expect(onSessionDone).toHaveBeenCalledWith('session-1');
@@ -444,9 +464,7 @@ describe('useSessionRunningStatus silenced completion handling', () => {
const onSessionNeedsReply = vi.fn();
renderHook(() => useSessionRunningStatus('another-session', { onSessionNeedsReply }));
- await emitSnapshot(
- new Map([['s-setup', { ...status(false), hasPendingPluginSetup: true }]]),
- );
+ await emitSnapshot(new Map([['s-setup', { ...status(false), hasPendingPluginSetup: true }]]));
expect(vi.mocked(addSessionAttention)).toHaveBeenCalledWith('s-setup', 'awaiting');
expect(onSessionNeedsReply).toHaveBeenCalledWith('s-setup');
@@ -462,7 +480,9 @@ describe('useSessionRunningStatus silenced completion handling', () => {
storeMock.terminalErrorSessions.delete('s-orphan');
await emitSnapshot(new Map([['s-orphan', status(true)]]));
- expect(vi.mocked(clearSessionAttention)).toHaveBeenCalledWith('s-orphan', { intent: 'explicit' });
+ expect(vi.mocked(clearSessionAttention)).toHaveBeenCalledWith('s-orphan', {
+ intent: 'explicit',
+ });
vi.mocked(getSessionAttentionKind).mockReturnValue(undefined);
});
@@ -473,7 +493,9 @@ describe('useSessionRunningStatus silenced completion handling', () => {
storeMock.terminalErrorSessions.add('s-real');
await emitSnapshot(new Map([['s-real', status(true)]]));
- expect(vi.mocked(clearSessionAttention)).not.toHaveBeenCalledWith('s-real', { intent: 'explicit' });
+ expect(vi.mocked(clearSessionAttention)).not.toHaveBeenCalledWith('s-real', {
+ intent: 'explicit',
+ });
vi.mocked(getSessionAttentionKind).mockReturnValue(undefined);
storeMock.terminalErrorSessions.delete('s-real');
});
@@ -566,5 +588,4 @@ describe('useSessionRunningStatus silenced completion handling', () => {
expect(onSessionDone).not.toHaveBeenCalled();
vi.useRealTimers();
});
-
});
diff --git a/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts b/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts
index 3e725c18da..d8ad5a5e27 100644
--- a/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts
+++ b/apps/desktop/src/renderer/hooks/useSessionRunningStatus.ts
@@ -53,7 +53,6 @@ import {
isSessionTerminalNotificationOwnedByScheduler,
isSessionDoneSilenced,
} from '@/lib/silencedSessionDoneStore';
-import { getStartingSessionIds } from '@/lib/sessionStartingStore';
// Codex maker 化后, codex session 也走 makerChatStore;
// 不再需要双 store 合并 —— 直接订阅 makerChatStore 即可。
@@ -134,9 +133,7 @@ export function useSessionRunningStatus(
* QUEUE_DEBOUNCE_MS 内若同 session 又开新 turn,取消这次 fire(是自动衔接);
* 时限内没起新 turn 才真正弹通知 + 亮角标。
*/
- const pendingDoneTimersRef = useRef(
- new Map<string, ReturnType<typeof setTimeout>>(),
- );
+ const pendingDoneTimersRef = useRef(new Map<string, ReturnType<typeof setTimeout>>());
const notifications = useSessionAttentionSnapshot();
@@ -239,7 +236,11 @@ export function useSessionRunningStatus(
const cur = makerChatStore.getRunningSnapshot().get(sessionId);
const isActive = sessionId === activeSessionIdRef.current;
const isRunning = cur?.isRunning === true;
- const isStarting = getStartingSessionIds().has(sessionId);
+ // debounce 期间下一轮可能在真正进入 running 前失败并写入 terminal error。
+ // error 红点由现有告警 owner 投影;这里必须避免旧 run 的延迟 done 把它
+ // 覆盖成橙点,或再发一条过期的完成通知。starting 本身不作为抑制条件:
+ // 侧栏已把它显示为 running,保留旧 done 才能在启动失败时继续提醒用户。
+ const hasTerminalError = makerChatStore.hasSessionTerminalError(sessionId);
const stillPending =
!!cur &&
(cur.hasPendingAskUser ||
@@ -250,12 +251,14 @@ export function useSessionRunningStatus(
!wasActiveAtCompletion &&
!isActive &&
!isRunning &&
- !isStarting &&
+ !hasTerminalError &&
!stillPending
) {
addSessionAttention(sessionId, 'done');
}
- if (!notificationOwnedByScheduler) onSessionDoneRef.current?.(sessionId);
+ if (!notificationOwnedByScheduler && !hasTerminalError) {
+ onSessionDoneRef.current?.(sessionId);
+ }
}, QUEUE_DEBOUNCE_MS);
pendingDoneTimersRef.current.set(sessionId, timer);
}
@@ -345,13 +348,10 @@ export function useSessionRunningStatus(
}
}, [activeSessionId]);
- const clearNotification = useCallback(
- (sessionId: string) => {
- // 默认 passive:点击会话卡片只导航,'error' 红角标由 store 保住。
- clearSessionAttention(sessionId);
- },
- [],
- );
+ const clearNotification = useCallback((sessionId: string) => {
+ // 默认 passive:点击会话卡片只导航,'error' 红角标由 store 保住。
+ clearSessionAttention(sessionId);
+ }, []);
// Derive running set for the return value (outside effect, for rendering).
// 必须 memo:裸调用每渲染都 new Set,会顺着 effectiveRunningSessionIds →
@@ -371,9 +371,7 @@ export function useSessionRunningStatus(
}
/** Extract the set of currently-running session IDs from the status map. */
-function deriveRunningSet(
- statusMap: ReadonlyMap<string, SessionStatusInfo>,
-): ReadonlySet<string> {
+function deriveRunningSet(statusMap: ReadonlyMap<string, SessionStatusInfo>): ReadonlySet<string> {
const set = new Set<string>();
for (const [id, info] of statusMap) {
if (info.isRunning) set.add(id);