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): bound remote cold start fallback

MagicLizif8aa0b0534c3
diff --git a/apps/mobile/app/resources/[collectionId].tsx b/apps/mobile/app/resources/[collectionId].tsx
index 6b673bf176..72764dfeb1 100644
--- a/apps/mobile/app/resources/[collectionId].tsx
+++ b/apps/mobile/app/resources/[collectionId].tsx
@@ -114,7 +114,7 @@ function RemoteCollectionScreenContent() {
title={title || t('devices.resources.titleFallback')}
titleTestID="remoteResources.title"
/>
- {loading ? (
+ {loading && items.length === 0 ? (
<View style={styles.center}>
<RemoteListSyncingPlaceholder testID="remoteResources.loading" />
</View>
diff --git a/apps/mobile/src/__tests__/TeammateHomeScreen.test.tsx b/apps/mobile/src/__tests__/TeammateHomeScreen.test.tsx
index 4a492a6144..176c564f38 100644
--- a/apps/mobile/src/__tests__/TeammateHomeScreen.test.tsx
+++ b/apps/mobile/src/__tests__/TeammateHomeScreen.test.tsx
@@ -97,6 +97,16 @@ beforeEach(() => {
});
afterEach(() => { act(() => root?.unmount()); root = undefined; });
describe('teammate home entry', () => {
+ it('hides an unverified cached roster while keeping cloud-only rows available', async () => {
+ h.roster.loading = true; h.roster.error = null;
+ await render();
+ expect(h.list.items).toEqual([]);
+ h.cloud.supported = true;
+ h.cloud.actors = [{ id: 'actor', kind: 'bot', externalId: 'writer', name: 'Writer', avatar: null, homeDeviceId: 'mac' }];
+ await render();
+ expect(h.list.items).toHaveLength(1);
+ expect(h.list.items[0].item.revision).toBe('cloud');
+ });
it('lands on the roster and never opens the remembered teammate on its own', async () => {
// A remembered, online teammate on a settled roster used to be reopened at launch (#4912); the
// roster is the destination now, in every focus, refresh and remount.
diff --git a/apps/mobile/src/__tests__/useRemoteResourceList.test.tsx b/apps/mobile/src/__tests__/useRemoteResourceList.test.tsx
index 2a89f5faf0..4c4af79654 100644
--- a/apps/mobile/src/__tests__/useRemoteResourceList.test.tsx
+++ b/apps/mobile/src/__tests__/useRemoteResourceList.test.tsx
@@ -3,6 +3,7 @@ import { act, createElement } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { HostedRemoteCollectionItem, RemoteResourceHostTarget } from '@/device-link/remoteResources';
+import { REMOTE_RESOURCE_STARTUP_TIMEOUT_MS } from '@/device-link/remoteResourceAvailability';
const h = vi.hoisted(() => ({
auth: { user: { id: 'owner' }, accountGeneration: 1 }, focused: true,
link: { connectionEpoch: 1, status: 'online', presenceVersion: 1, getPresenceAvailability: vi.fn(() => true as boolean | null),
@@ -181,6 +182,24 @@ it('shows disk-cached rows during the first slow network read without claiming a
expect(result.isOnline(targets[0])).toBe(false);
});
+it('ends a stalled cold relay wait with cached rows and retries after reconnect', async () => {
+ vi.useFakeTimers();
+ h.cached = [cached(targets[0], 'cached-startup')];
+ h.link.status = 'connecting';
+ await render();
+ expect(h.list).not.toHaveBeenCalled();
+ expect(result.loading).toBe(true);
+ await act(async () => vi.advanceTimersByTimeAsync(REMOTE_RESOURCE_STARTUP_TIMEOUT_MS));
+ expect(result.loading).toBe(false);
+ expect(result.error).toContain('devices.resources.hostOffline');
+ expect(result.items.map(row => row.item.ref.id)).toEqual(['cached-startup']);
+ h.link.status = 'online'; h.link.connectionEpoch++;
+ h.list.mockResolvedValue({ items: [item('fresh-after-reconnect')] });
+ await render();
+ expect(h.list).toHaveBeenCalled();
+ expect(result.error).toBeNull();
+});
+
it('commits and persists a successful host before a slow sibling settles', async () => {
let finish!: (value: unknown) => void;
h.list.mockImplementation(async (_invoke, host) => host.deviceId === 'mac' ? { items: [item('fresh-mac')] }
diff --git a/apps/mobile/src/__tests__/useTeammateRoster.test.tsx b/apps/mobile/src/__tests__/useTeammateRoster.test.tsx
index 1b2991c695..a8f7b0c43c 100644
--- a/apps/mobile/src/__tests__/useTeammateRoster.test.tsx
+++ b/apps/mobile/src/__tests__/useTeammateRoster.test.tsx
@@ -20,6 +20,7 @@ vi.mock('@/device-link/revokedDevicesStore', () => ({ useRevokedDevices: () => h
vi.mock('@/device-link/remoteStatus', async () => ({ ...await vi.importActual<typeof import('@/device-link/remoteStatus')>('@/device-link/remoteStatus'), formatRemoteError: String }));
vi.mock('@/device-link/remoteResourceCache', () => ({ cacheRemoteResourceHome: vi.fn(), readRemoteResourceSnapshot: h.snapshot }));
vi.mock('@/session/useRemoteResourceList', () => ({ useRemoteResourceList: () => h.list }));
+import { REMOTE_RESOURCE_STARTUP_TIMEOUT_MS } from '@/device-link/remoteResourceAvailability';
import { useTeammateRoster } from '@/session/useTeammateRoster';
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root | undefined;
@@ -62,6 +63,24 @@ it('keeps a cold launch loading while the relay is starting, without reporting c
expect(result.error).toBeNull();
expect(h.link.readDeviceList).not.toHaveBeenCalled();
});
+it('falls back to the cached roster after a stalled relay startup and recovers on reconnect', async () => {
+ vi.useFakeTimers();
+ const cached = { key: 'old:bot', host: { deviceId: 'old', deviceName: 'Old' }, item: {
+ ref: { collectionId: 'teammates', kind: 'bot', id: 'bot' }, revision: '1', display: { title: 'Cached' }, links: [],
+ } };
+ h.link.status = 'connecting';
+ h.list.items = [cached];
+ h.snapshot.mockResolvedValue({ home: [], items: { teammates: [cached] }, read: {} });
+ await render();
+ expect(result.loading).toBe(true);
+ await act(async () => vi.advanceTimersByTimeAsync(REMOTE_RESOURCE_STARTUP_TIMEOUT_MS));
+ expect(result.loading).toBe(false);
+ expect(result.items).toEqual([cached]);
+ expect(result.error).toContain('devices.resources.hostOffline');
+ h.link.status = 'online'; h.link.connectionEpoch++;
+ await render();
+ expect(h.link.readDeviceList).toHaveBeenCalled();
+});
it('drops old account discovery replies', async () => {
let finish!: (value: unknown) => void;
h.link.readDeviceList.mockImplementationOnce(() => new Promise(resolve => { finish = resolve; }));
diff --git a/apps/mobile/src/device-link/remoteResourceAvailability.ts b/apps/mobile/src/device-link/remoteResourceAvailability.ts
index 7ad9cbaa7c..face2ed726 100644
--- a/apps/mobile/src/device-link/remoteResourceAvailability.ts
+++ b/apps/mobile/src/device-link/remoteResourceAvailability.ts
@@ -1,5 +1,8 @@
import type { HostedRemoteCollectionItem } from './remoteResources';
+/** Give a cold relay a bounded window before falling back to cached remote rows. */
+export const REMOTE_RESOURCE_STARTUP_TIMEOUT_MS = 10_000;
+
/** A host reply is evidence only for the connection in which it arrived. */
export function isRemoteResourceHostOnline(
relayStatus: string,
diff --git a/apps/mobile/src/session/TeammateHomeScreen.tsx b/apps/mobile/src/session/TeammateHomeScreen.tsx
index 1ba9a983ff..66514724cf 100644
--- a/apps/mobile/src/session/TeammateHomeScreen.tsx
+++ b/apps/mobile/src/session/TeammateHomeScreen.tsx
@@ -53,8 +53,10 @@ export function TeammateHomeScreen({ active = true }: { active?: boolean }) {
// Main computers (cloud-companions.md): only with a server that declares companionHome.
const cloud = useCloudCompanionHome(focused);
// One row per companion, under its main computer (a row built from the cloud when none is listed).
- const items = useMemo(() => cloud.supported ? cloudCompanionRows(roster.items, cloud.actors, cloud.deviceNames) : roster.items,
- [cloud.actors, cloud.deviceNames, cloud.supported, roster.items]);
+ // Unverified disk rows stay hidden on the first roster load; cloud actors remain independently usable.
+ const visibleRosterItems = roster.loading && !roster.error ? [] : roster.items;
+ const items = useMemo(() => cloud.supported ? cloudCompanionRows(visibleRosterItems, cloud.actors, cloud.deviceNames) : visibleRosterItems,
+ [cloud.actors, cloud.deviceNames, cloud.supported, visibleRosterItems]);
// A cloud companion opens its normal chat (cached when its computer is unreachable). Only a row
// this phone knows from the cloud alone (never listed by its computer) opens the cloud conversation.
const cloudActor = (row: HostedRemoteCollectionItem) => cloud.supported && row.item.revision === CLOUD_ROW_REVISION ? cloudActorForRow(cloud.actors, row) : null;
diff --git a/apps/mobile/src/session/useRemoteResourceList.ts b/apps/mobile/src/session/useRemoteResourceList.ts
index 1717e0992b..352338e526 100644
--- a/apps/mobile/src/session/useRemoteResourceList.ts
+++ b/apps/mobile/src/session/useRemoteResourceList.ts
@@ -9,7 +9,7 @@ import {
cacheRemoteResourceItems, readRemoteResourceSnapshot,
remoteResourceCacheRevision, subscribeRemoteResourceCache,
} from '@/device-link/remoteResourceCache';
-import { remoteResourceConnectionState, hasRemoteCollectionCache, isRemoteResourceHostOnline, readRemoteCollectionCache, writeRemoteCollectionCache } from '@/device-link/remoteResourceAvailability';
+import { REMOTE_RESOURCE_STARTUP_TIMEOUT_MS, remoteResourceConnectionState, hasRemoteCollectionCache, isRemoteResourceHostOnline, readRemoteCollectionCache, writeRemoteCollectionCache } from '@/device-link/remoteResourceAvailability';
import { listRemoteCollection, mergeRemoteCollectionHostShards, normalizeRemoteCollectionItems,
type HostedRemoteCollectionItem, type RemoteResourceHostTarget } from '@/device-link/remoteResources';
import { formatRemoteError, isAutoRecoveringRemoteError } from '@/device-link/remoteStatus';
@@ -56,6 +56,16 @@ export function useRemoteResourceList(collectionId: string, targets: readonly Re
return () => { cancelled = true; generation.current += 1; };
}, [binding, collectionId, owner, user?.id]);
+ useEffect(() => {
+ if (!enabled || hydrated !== binding || state.settled || (status !== 'stopped' && status !== 'connecting')) return;
+ const timer = setTimeout(() => {
+ setState((old) => old.binding === binding && !old.settled ? {
+ ...old, settled: true, loading: false, refreshing: false, error: t('devices.resources.hostOffline'),
+ } : old);
+ }, REMOTE_RESOURCE_STARTUP_TIMEOUT_MS);
+ return () => clearTimeout(timer);
+ }, [binding, enabled, hydrated, state.settled, status, t]);
+
const read = useCallback(async (run: RemoteSyncRun) => {
if (!enabled || !active.current || hydrated !== binding || !collectionId) return;
// The relay starts as stopped/connecting during a cold launch. Do not
diff --git a/apps/mobile/src/session/useTeammateRoster.ts b/apps/mobile/src/session/useTeammateRoster.ts
index a84eb50d43..22188fa1a2 100644
--- a/apps/mobile/src/session/useTeammateRoster.ts
+++ b/apps/mobile/src/session/useTeammateRoster.ts
@@ -1,4 +1,4 @@
-import { useCallback, useRef, useState } from 'react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import { AppState } from 'react-native';
import { REMOTE_RESOURCE_MANIFEST_CHANNEL, type RemoteResourceManifestResponse } from '@cindy/device-link';
import { useFocusEffect } from 'expo-router';
@@ -8,7 +8,7 @@ import { useDeviceLink } from '@/device-link/DeviceLinkContext';
import { toDeviceListItems } from '@/device-link/devices';
import { useRevokedDevices } from '@/device-link/revokedDevicesStore';
import { cacheRemoteResourceHome, readRemoteResourceSnapshot } from '@/device-link/remoteResourceCache';
-import { readRemoteCollectionCache } from '@/device-link/remoteResourceAvailability';
+import { REMOTE_RESOURCE_STARTUP_TIMEOUT_MS, readRemoteCollectionCache } from '@/device-link/remoteResourceAvailability';
import { discoverRemoteHomeCollections, remoteResourceDiscoveryTargets,
type RemoteHomeCollection, type RemoteResourceHostTarget } from '@/device-link/remoteResources';
import { formatRemoteError, isAutoRecoveringRemoteError } from '@/device-link/remoteStatus';
@@ -157,6 +157,16 @@ export function useTeammateRoster(enabled = true) {
targets: old.binding === binding && JSON.stringify(old.targets) === JSON.stringify(filtered) ? old.targets : filtered,
groupTargets, error: error ?? (failures.length ? formatRemoteError(failures[0]) : null) }));
}, [binding, i18n.language, invoke, openLink, readDeviceList, revoked, status, t, user?.id]);
+ useEffect(() => {
+ if (!enabled || discovery.binding !== binding || !discovery.ready || discovery.settled
+ || (status !== 'stopped' && status !== 'connecting')) return;
+ const timer = setTimeout(() => {
+ setDiscovery((old) => old.binding === binding && !old.settled ? {
+ ...old, settled: true, loading: false, error: t('devices.resources.hostOffline'),
+ } : old);
+ }, REMOTE_RESOURCE_STARTUP_TIMEOUT_MS);
+ return () => clearTimeout(timer);
+ }, [binding, discovery.binding, discovery.ready, discovery.settled, enabled, status, t]);
const requestDiscover = useRemoteSyncCoordinator(discover, JSON.stringify([binding, connectionEpoch, presenceVersion, status, enabled]));
useFocusEffect(useCallback(() => {
if (!enabled) return;
diff --git a/docs/product-rules/cindy-bots-completion-matrix.md b/docs/product-rules/cindy-bots-completion-matrix.md
index ec071a67cd..1baba9b35f 100644
--- a/docs/product-rules/cindy-bots-completion-matrix.md
+++ b/docs/product-rules/cindy-bots-completion-matrix.md
@@ -54,7 +54,7 @@
## 2026-09-06 手机与另一台电脑的补齐范围
-- 多设备名册:来源电脑与伙伴身份共同定位;保存账号隔离的名册缓存,冷启动先显示加载中,首轮权威同步完成后再显示最新名册;已完成首轮同步后断线才按缓存展示。单个来源失败只影响该来源,重新连接后重新读取,旧请求不能覆盖停连或换账号后的状态。
+- 多设备名册:来源电脑与伙伴身份共同定位;保存账号隔离的名册缓存,冷启动先显示加载中,首轮权威同步完成后再显示最新名册;relay 启动等待超时则回退到缓存并显示可重试的错误;已完成首轮同步后断线才按缓存展示。单个来源失败只影响该来源,重新连接后重新读取,旧请求不能覆盖停连或换账号后的状态。
- 后台任务:手机与另一台 Desktop 均可从伙伴时间线读取任务状态、打开来源电脑的子任务、停止任务;状态推送沿父任务订阅回流。远端只得到任务卡所需字段,不输出数据库行或冻结的权限配置。
- 伙伴私聊:两端从原消息入口打开来源电脑的只读私聊,收到更新或重连时刷新;不会以控制端本机的同名伙伴替换远端身份。
- 手机呈现:解析与 Desktop 共用的持久任务/私聊标记,显示预设和托管图片头像,恢复成果文件入口与运行模型展示。主任务记录发生恢复替换时,聚焦/重连/资源更新重新解析伙伴身份。