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(mobile): 确认放弃发送时同步标记,避免与上传完成竞态

Dashd2fa289ac623
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index 8fa52ba80a..de56ade0f8 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -612,7 +612,7 @@ export default function NewRemoteSessionScreen() {
const [creating, setCreating] = useState(false);
const [createStartedAt, setCreateStartedAt] = useState<number | null>(null);
const [createPhase, setCreatePhase] = useState<SlowSendPhase>('preparing');
- useConfirmLeaveWhileUploading(creating && createPhase === 'uploading');
+ const leaveConfirmedRef = useConfirmLeaveWhileUploading(creating && createPhase === 'uploading');
const [error, setError] = useState<string | null>(() => {
const creationError = readRouteString(params.creationError);
return creationError ? t('session.screen.createFailedNotice', { message: creationError }) : null;
@@ -4528,7 +4528,7 @@ export default function NewRemoteSessionScreen() {
// 此时不该带着残缺附件去开新会话。等待期间离开会先确认;确认离开后在途上传随页面丢弃,同样放弃。
setCreatePhase('uploading');
const { failedCount, abandoned } = await waitForPendingUploads();
- if (abandoned) return;
+ if (abandoned || leaveConfirmedRef.current) return;
if (!isCurrentOwner()) return;
if (!ensureDeviceAlive()) return;
if (failedCount > 0) return;
diff --git a/apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx b/apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx
index 3270b502a8..7cf2e43564 100644
--- a/apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx
+++ b/apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx
@@ -106,7 +106,7 @@ vi.mock('@/components/AppText', () => ({
TextInput: ({ testID, ...props }: any) => { h.inputs[testID] = props; return el('input', { 'data-testid': testID, value: props.value ?? '', readOnly: true }); },
}));
vi.mock('@/components/PaperPlaneIcon', () => ({ PaperPlaneIcon: () => null }));
-vi.mock('@/session/useConfirmLeaveWhileUploading', () => ({ useConfirmLeaveWhileUploading: (active: boolean) => { h.leaveGuard.push(active); } }));
+vi.mock('@/session/useConfirmLeaveWhileUploading', () => ({ useConfirmLeaveWhileUploading: (active: boolean) => { h.leaveGuard.push(active); return { current: false }; } }));
vi.mock('@/components/MobilePrimitives', () => ({
MainWindowActionButton: ({ action }: any) => el('button', {
'data-testid': action.testID, 'aria-label': action.accessibilityLabel, 'aria-busy': action.busy ? 'true' : undefined,
diff --git a/apps/mobile/src/__tests__/useConfirmLeaveWhileUploading.test.tsx b/apps/mobile/src/__tests__/useConfirmLeaveWhileUploading.test.tsx
new file mode 100644
index 0000000000..07c400fd75
--- /dev/null
+++ b/apps/mobile/src/__tests__/useConfirmLeaveWhileUploading.test.tsx
@@ -0,0 +1,59 @@
+// @vitest-environment jsdom
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { useConfirmLeaveWhileUploading } from '@/session/useConfirmLeaveWhileUploading';
+
+const h = vi.hoisted(() => ({
+ prevent: vi.fn(),
+ alert: vi.fn(),
+ dispatch: vi.fn(),
+}));
+
+vi.mock('react-native', () => ({ Alert: { alert: h.alert } }));
+vi.mock('expo-router', () => ({ useNavigation: () => ({ dispatch: h.dispatch }) }));
+vi.mock('expo-router/react-navigation', () => ({ usePreventRemove: h.prevent }));
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+
+let root: Root;
+beforeEach(() => {
+ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
+ vi.clearAllMocks();
+ root = createRoot(document.createElement('div'));
+});
+afterEach(() => act(() => root.unmount()));
+
+function mount(active: boolean) {
+ let confirmed!: { current: boolean };
+ function Harness() {
+ confirmed = useConfirmLeaveWhileUploading(active);
+ return null;
+ }
+ act(() => root.render(<Harness />));
+ const [enabled, onPrevent] = h.prevent.mock.calls.at(-1)!;
+ return { enabled, onPrevent, confirmed };
+}
+
+describe('useConfirmLeaveWhileUploading', () => {
+ it('only blocks leaving while a send waits on uploads', () => {
+ expect(mount(false).enabled).toBe(false);
+ expect(mount(true).enabled).toBe(true);
+ });
+
+ it('keeps waiting on cancel and leaves, marking the send abandoned, on discard', () => {
+ const { onPrevent, confirmed } = mount(true);
+ const action = { type: 'GO_BACK' };
+ onPrevent({ data: { action } });
+ const [, , buttons] = h.alert.mock.calls[0];
+ const stay = buttons.find((b: { style?: string }) => b.style === 'cancel');
+ const discard = buttons.find((b: { style?: string }) => b.style === 'destructive');
+
+ stay.onPress?.();
+ expect(h.dispatch).not.toHaveBeenCalled();
+ expect(confirmed.current).toBe(false);
+
+ discard.onPress();
+ expect(h.dispatch).toHaveBeenCalledWith(action);
+ expect(confirmed.current).toBe(true);
+ });
+});
diff --git a/apps/mobile/src/session/BotGroupComposer.tsx b/apps/mobile/src/session/BotGroupComposer.tsx
index bb01173002..4f3d30487e 100644
--- a/apps/mobile/src/session/BotGroupComposer.tsx
+++ b/apps/mobile/src/session/BotGroupComposer.tsx
@@ -101,7 +101,7 @@ export function BotGroupComposer({
const [division, setDivision] = useState(false);
const [sending, setSending] = useState(false);
const [waitingUploads, setWaitingUploads] = useState(false);
- useConfirmLeaveWhileUploading(waitingUploads);
+ const leaveConfirmedRef = useConfirmLeaveWhileUploading(waitingUploads);
const [stopping, setStopping] = useState(false);
const inputRef = useRef<NativeTextInput>(null);
const textRef = useRef(text); textRef.current = text;
@@ -197,7 +197,7 @@ export function BotGroupComposer({
setWaitingUploads(true);
const { failedCount, abandoned } = await tray.waitForPendingUploads();
setWaitingUploads(false);
- if (abandoned) return;
+ if (abandoned || leaveConfirmedRef.current) return;
const attachments = attachmentsSupported ? [...tray.attachmentsRef.current] : [];
if (failedCount > 0 || (!draftText && attachments.length === 0)) {
restoreDraft();
diff --git a/apps/mobile/src/session/useConfirmLeaveWhileUploading.ts b/apps/mobile/src/session/useConfirmLeaveWhileUploading.ts
index 66707d9f45..e380bba407 100644
--- a/apps/mobile/src/session/useConfirmLeaveWhileUploading.ts
+++ b/apps/mobile/src/session/useConfirmLeaveWhileUploading.ts
@@ -1,15 +1,20 @@
import { usePreventRemove } from 'expo-router/react-navigation';
import { useNavigation } from 'expo-router';
+import { useRef, type MutableRefObject } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert } from 'react-native';
/**
* 发送正在等附件上传时拦截所有离开方式(顶栏返回、iOS 边缘右滑、Android 返回键)。
* 离开会丢弃在途上传,这条消息随之放弃发送,先让用户确认。
+ *
+ * 返回的 ref 在用户确认放弃的瞬间同步置 true:导航卸载是异步的,上传恰好在这之间
+ * 完成时,等待方可能以 abandoned:false 醒来,需据此 ref 一并放弃。
*/
-export function useConfirmLeaveWhileUploading(active: boolean): void {
+export function useConfirmLeaveWhileUploading(active: boolean): MutableRefObject<boolean> {
const { t } = useTranslation();
const navigation = useNavigation();
+ const leaveConfirmedRef = useRef(false);
usePreventRemove(active, ({ data }) => {
Alert.alert(
t('session.leaveWhileUploading.title'),
@@ -19,9 +24,13 @@ export function useConfirmLeaveWhileUploading(active: boolean): void {
{
text: t('session.leaveWhileUploading.discard'),
style: 'destructive',
- onPress: () => navigation.dispatch(data.action),
+ onPress: () => {
+ leaveConfirmedRef.current = true;
+ navigation.dispatch(data.action);
+ },
},
],
);
});
+ return leaveConfirmedRef;
}