Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): 合并主干并保留连接提示恢复边界
Dash74f3a3a0bf60
diff --cc apps/mobile/src/__tests__/connectionBannerRecovery.test.tsxindex dab0c0d0b6,0000000000..55d164bd0dmode 100644,000000..100644--- a/apps/mobile/src/__tests__/connectionBannerRecovery.test.tsx+++ b/apps/mobile/src/__tests__/connectionBannerRecovery.test.tsx@@@ -1,56 -1,0 +1,61 @@@+// @vitest-environment jsdom+import { act, createElement } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, beforeEach, expect, it, vi } from 'vitest';++import { useDelayedConnectionNotice } from '@/components/ConnectionNoticeOverlay';+import { useShowConnectionBanner } from '@/components/ConnectionBanner';+import type { DeviceLinkStatus } from '@cindy/device-link';+++vi.mock('expo-router', () => ({ useFocusEffect: () => {} }));+vi.mock('react-native', () => ({ ActivityIndicator: () => null, View: () => null, StyleSheet: { create: (s: unknown) => s } }));+vi.mock('lucide-react-native', () => ({ LoaderCircle: () => null }));+vi.mock('@/hooks/useReduceMotion', () => ({ useReduceMotionEnabled: () => true }));+vi.mock('@/components/AppText', () => ({ Text: () => null }));+vi.mock('@/components/MobilePrimitives', () => ({ MainWindowActionButton: () => null, StatusDot: () => null }));+vi.mock('@/theme', () => ({ fontWeight: {}, useTheme: () => ({}), useThemedStyles: () => ({}) }));++let root: Root;+let visible = false;+beforeEach(() => {+ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });+ vi.useFakeTimers();+ root = createRoot(document.createElement('div'));+});+afterEach(() => { act(() => root.unmount()); vi.useRealTimers(); });+function Probe({ status, recovery, error, unresponsive }: {+ status: DeviceLinkStatus; recovery?: 'syncing' | 'recovered'; error?: string; unresponsive?: boolean;+}) {- visible = useShowConnectionBanner(status, error ?? null, null, unresponsive, recovery);++ const active = useShowConnectionBanner(status, error ?? null, null, unresponsive);++ visible = useDelayedConnectionNotice(active, recovery === 'recovered', recovery === 'syncing');+ return null;+}+function render(status: DeviceLinkStatus, recovery?: 'syncing' | 'recovered', error?: string, unresponsive?: boolean) {+ act(() => root.render(createElement(Probe, { status, recovery, error, unresponsive })));+}+const advance = (ms: number) => act(() => vi.advanceTimersByTime(ms));++it('keeps repeated online task loads quiet, even when content takes several seconds', () => {+ for (let visit = 0; visit < 3; visit++) {+ render('online', 'syncing'); advance(5_000); expect(visible).toBe(false);+ render('online', 'recovered'); advance(500); expect(visible).toBe(false);+ }+});+it('keeps a real outage visible through content repair, then clears the recovered tail', () => {- render('connecting', 'syncing'); advance(1_199); expect(visible).toBe(false);++ render('connecting', 'syncing'); advance(999); expect(visible).toBe(false);+ advance(1); expect(visible).toBe(true);+ render('online', 'syncing'); advance(5_000); expect(visible).toBe(true);+ render('online', 'recovered'); advance(1_999); expect(visible).toBe(true);+ advance(1); expect(visible).toBe(false);+ render('online', 'syncing'); advance(5_000); expect(visible).toBe(false);+});+it('does not promote a brief connection blip into a long content recovery banner', () => {+ render('connecting', 'syncing'); advance(300);+ render('online', 'syncing'); advance(5_000); expect(visible).toBe(false);+});- it('still shows actual request errors and an unresponsive computer immediately', () => {- render('online', 'syncing', 'INVOKE_TIMEOUT'); expect(visible).toBe(true);++it('shows real errors after the shared delay and retains their recovery tail', () => {++ render('online', 'syncing', 'INVOKE_TIMEOUT'); expect(visible).toBe(false);++ advance(1_000); expect(visible).toBe(true);+ render('online', 'syncing', undefined, true); expect(visible).toBe(true);- render('online', 'recovered'); expect(visible).toBe(false);++ render('online', 'recovered'); expect(visible).toBe(true);++ advance(2_000); expect(visible).toBe(false);+});diff --cc apps/mobile/src/__tests__/connectionNoticeDelay.test.tsindex 0000000000,cb217a51d0..f95d018ad8mode 000000,100644..100644--- a/apps/mobile/src/__tests__/connectionNoticeDelay.test.ts+++ b/apps/mobile/src/__tests__/connectionNoticeDelay.test.ts@@@ -1,0 -1,60 +1,60 @@@+ import { afterEach, describe, expect, it, vi } from 'vitest';+ import { readFileSync } from 'node:fs';+ import { scheduleConnectionNotice, updateConnectionNoticeVisibility } from '@/components/connectionNoticeDelay';++ describe('floating connection notice', () => {+ afterEach(() => vi.useRealTimers());+ it('waits one second and cancels a brief outage without flashing', () => {+ vi.useFakeTimers();+ const reveal = vi.fn();+ const cancel = scheduleConnectionNotice(reveal);+ vi.advanceTimersByTime(999);+ expect(reveal).not.toHaveBeenCalled();+ cancel();+ vi.advanceTimersByTime(1_000);+ expect(reveal).not.toHaveBeenCalled();+ const cancelNext = scheduleConnectionNotice(reveal);+ vi.advanceTimersByTime(999);+ expect(reveal).not.toHaveBeenCalled();+ vi.advanceTimersByTime(1);+ expect(reveal).toHaveBeenCalledTimes(1);+ cancelNext();+ });+ it('keeps the anchor out of layout and renders touch targets in the root overlay', () => {+ const overlay = readFileSync('src/components/ConnectionNoticeOverlay.tsx', 'utf8');+ expect(overlay).toContain('anchor: { height: 0 }');+ expect(overlay).toContain('pointerEvents="box-none" style={styles.layer}');+ expect(overlay).toContain('context?.publish(id, null)');+ const root = readFileSync('app/_layout.tsx', 'utf8');+ expect(root).toContain('<ConnectionNoticeProvider>{body}</ConnectionNoticeProvider>');+ const banner = readFileSync('src/components/ConnectionBanner.tsx', 'utf8');- expect(banner).toContain("useDelayedConnectionNotice(cachedOnly || active, recovery === 'recovered')");++ expect(banner).toContain("useDelayedConnectionNotice(cachedOnly || active, recovery === 'recovered', recovery === 'syncing')");+ expect(banner).toContain('<ConnectionNoticeOverlay>');+ });+ it('holds completion for two seconds only after a visible incident and cancels it on a new outage', () => {+ vi.useFakeTimers();+ let visible = false;+ let cancel: (() => void) | undefined;+ const update = (active: boolean, completed: boolean) => {+ cancel?.();+ cancel = updateConnectionNoticeVisibility(active, completed, visible, (next) => { visible = next; });+ };+ update(true, false);+ vi.advanceTimersByTime(500);+ update(false, true);+ vi.advanceTimersByTime(2_000);+ expect(visible).toBe(false);+ update(true, false);+ vi.advanceTimersByTime(1_000);+ expect(visible).toBe(true);+ update(false, true);+ vi.advanceTimersByTime(1_999);+ expect(visible).toBe(true);+ update(true, false);+ vi.advanceTimersByTime(2_000);+ expect(visible).toBe(true);+ update(false, true);+ vi.advanceTimersByTime(2_000);+ expect(visible).toBe(false);+ });+ });diff --cc apps/mobile/src/components/ConnectionBanner.tsxindex 5d93a6aa0c,fab1c0bac2..317718ed51--- a/apps/mobile/src/components/ConnectionBanner.tsx+++ b/apps/mobile/src/components/ConnectionBanner.tsx@@@ -41,24 -31,10 +31,10 @@@ export function useShowConnectionBannerdeviceUnresponsive = false,recovery?: 'syncing' | 'recovered',): boolean {- const offline = status !== 'online' || recovery === 'syncing';- return recovery === 'recovered' || resolveConnectionBannerVisibility({+ const offline = status !== 'online';- const showRecovered = recovery !== undefined;- const [offlineLongEnough, setOfflineLongEnough] = useState(false);- useEffect(() => {- if (!offline) {- // Opening a task is content loading, not a lost connection. Only keep a- // banner that was already shown for a real outage until content catches up.- if (recovery === 'syncing') return;- if (!showRecovered) { setOfflineLongEnough(false); return; }- const timer = setTimeout(() => setOfflineLongEnough(false), 2_000);- return () => clearTimeout(timer);- }- const timer = setTimeout(() => setOfflineLongEnough(true), OFFLINE_BANNER_DELAY_MS);- return () => clearTimeout(timer);- }, [offline, showRecovered, recovery]);- return (recovery !== undefined && offlineLongEnough) || resolveConnectionBannerVisibility({++ return recovery !== undefined || resolveConnectionBannerVisibility({offline,- offlineLongEnough,+ offlineLongEnough: true,// 熔断已关后屏幕残留的 DEVICE_UNRESPONSIVE 错误按陈旧丢弃(review P1),// 否则恢复后 banner 会带着"自动重试中"文案常驻到用户手动同步。hasError: Boolean(resolveEffectiveConnectionError(error, deviceUnresponsive)),@@@ -99,9 -75,9 +75,9 @@@ export function ConnectionBanner(cachedOnly?: boolean;}) {const styles = useThemedStyles(makeStyles);- const { colors } = useTheme();- const reduceMotion = useReduceMotionEnabled();const { t } = useTranslation();- const active = useShowConnectionBanner(status, error, issue, deviceUnresponsive, recovery === 'recovered' ? undefined : recovery);- const visible = useDelayedConnectionNotice(cachedOnly || active, recovery === 'recovered');++ const active = useShowConnectionBanner(status, error, issue, deviceUnresponsive);++ const visible = useDelayedConnectionNotice(cachedOnly || active, recovery === 'recovered', recovery === 'syncing');// 链路已 online 说明普通 issue 已过期;unstable 描述跨连接抖动,online 时仍展示。// issue 优先于请求级 error:链路断因明确时,invoke 失败都是它的下游症状(NOT_CONNECTED)。const activeIssue = status !== 'online' || issue?.kind === 'unstable' ? issue : null;diff --cc apps/mobile/src/components/ConnectionNoticeOverlay.tsxindex 0000000000,379e978bca..f4d4fb81b5mode 000000,100644..100644--- a/apps/mobile/src/components/ConnectionNoticeOverlay.tsx+++ b/apps/mobile/src/components/ConnectionNoticeOverlay.tsx@@@ -1,0 -1,71 +1,73 @@@+ import { createContext, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, type ReactNode, type RefObject } from 'react';+ import { StyleSheet, View, useWindowDimensions } from 'react-native';+ import { useFocusEffect } from 'expo-router';+ import { spacing } from '@/theme/tokens';+ import { updateConnectionNoticeVisibility } from './connectionNoticeDelay';++ /** Each continuous incident gets one delay; clearing it cancels pending display. */-export function useDelayedConnectionNotice(active: boolean, completed = false): boolean {++export function useDelayedConnectionNotice(active: boolean, completed = false, retainVisible = false): boolean {+ const [ready, setReady] = useState(false);++ // Content repair can retain an already visible incident, but cannot start one.++ const effectiveActive = active || (retainVisible && ready);+ useEffect(() => {- return updateConnectionNoticeVisibility(active, completed, ready, setReady);- }, [active, completed, ready]);- return (active || completed) && ready;++ return updateConnectionNoticeVisibility(effectiveActive, completed, ready, setReady);++ }, [effectiveActive, completed, ready]);++ return (effectiveActive || completed) && ready;+ }++ type Notice = { top: number; children: ReactNode };+ const NoticeContext = createContext<{ publish: (id: string, notice: Notice | null) => void; host: RefObject<View | null> } | null>(null);++ /** Render outside clipped headers, with real touch bounds and no modal input capture. */+ export function ConnectionNoticeProvider({ children }: { children: ReactNode }) {+ const host = useRef<View>(null);+ const [notices, setNotices] = useState(new Map<string, Notice>());+ const publish = useCallback((id: string, notice: Notice | null) => {+ setNotices((current) => {+ if (!notice && !current.has(id)) return current;+ const next = new Map(current);+ if (notice) next.set(id, notice); else next.delete(id);+ return next;+ });+ }, []);+ const value = useMemo(() => ({ publish, host }), [publish]);+ return <NoticeContext.Provider value={value}>+ <View ref={host} collapsable={false} style={styles.host}>+ {children}+ <View pointerEvents="box-none" style={styles.layer}>+ {[...notices].map(([id, notice]) => <View key={id} style={[styles.overlay, { top: notice.top }]}>{notice.children}</View>)}+ </View>+ </View>+ </NoticeContext.Provider>;+ }++ /** Only the zero-height measuring anchor participates in the screen layout. */+ export function ConnectionNoticeOverlay({ children }: { children: ReactNode }) {+ const context = useContext(NoticeContext);+ const id = useId();+ const anchor = useRef<View>(null);+ const [top, setTop] = useState<number | null>(null);+ const [focused, setFocused] = useState(false);+ useFocusEffect(useCallback(() => {+ setFocused(true);+ return () => setFocused(false);+ }, []));+ const { width, height } = useWindowDimensions();+ const measure = useCallback(() => {+ if (context?.host.current) anchor.current?.measureLayout(context.host.current, (_x, y) => setTop(y + spacing.sm));+ }, [context]);+ useEffect(() => { measure(); }, [measure, width, height, focused]);+ useEffect(() => {+ if (focused && top !== null) context?.publish(id, { top, children });+ else context?.publish(id, null);+ return () => context?.publish(id, null);+ }, [children, focused, id, context, top]);+ return <View ref={anchor} collapsable={false} pointerEvents="none" onLayout={measure} style={styles.anchor} />;+ }++ const styles = StyleSheet.create({+ host: { flex: 1 },+ layer: { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, zIndex: 20 },+ anchor: { height: 0 },+ overlay: { position: 'absolute', left: spacing.md, right: spacing.md },+ });