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): 生成任务链接时优先使用当前归属设备

Dash606d4b06d2a6
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index 6cd33103f9..79a70a5448 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -9423,8 +9423,8 @@ function SessionScreenContent() {
// 冻结当前任务所在设备:目标归档 / 未同步后,从别的电脑的任务里点开也能找到它。
const copyMessageLink = useCallback((clientId: string) => {
if (!sessionId) return;
- void copyMessageText(buildMobileSessionMessageDeepLink(sessionId, clientId, { deviceId }));
- }, [deviceId, sessionId]);
+ void copyMessageText(buildMobileSessionMessageDeepLink(sessionId, clientId, { session: currentSession, deviceId }));
+ }, [currentSession, deviceId, sessionId]);
const resolvePastedSessionLinkLabel = useCallback(async (href: string) => {
const target = parseSessionDeepLinkUrl(href);
@@ -9491,7 +9491,7 @@ function SessionScreenContent() {
const summary = target ? mobileSessionMessageDisplayText(target) : null;
const bounded = summary ? boundAgentReferenceText(summary) : null;
const node = sessionLinkComposerNode({
- href: buildMobileSessionMessageDeepLink(sessionId, clientId, { deviceId }),
+ href: buildMobileSessionMessageDeepLink(sessionId, clientId, { session: currentSession, deviceId }),
label: compactSessionMessageLabel(summary ?? clientId),
titled: true,
...(bounded?.text ? { agentText: bounded.text } : {}),
@@ -9501,7 +9501,7 @@ function SessionScreenContent() {
if (editor) editor.insertNode(node);
else applyComposerDocument(appendComposerNode(composerDocumentRef.current, node));
requestAnimationFrame(() => composerInputRef.current?.focus());
- }, [applyComposerDocument, canUseComposer, deviceId, messages, sessionId]);
+ }, [applyComposerDocument, canUseComposer, currentSession, deviceId, messages, sessionId]);
const deleteMessage = useCallback((clientId: string) => {
if (!deviceId || messageActionBusy) return;
diff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
index f78ec57c14..dc4fd8cfc4 100644
--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts
+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
@@ -3,6 +3,8 @@ import { MAKER_EVENT_BATCH_CHANNEL, SESSION_SYNC_CHANNEL } from '@cindy/device-l
import { clampLiveRowCreatedAt } from '@/session/messagePaging';
import { MOBILE_TOOL_INPUT_PROJECTION_THRESHOLD_BYTES } from '@/session/messageToolPayloadProjection';
import { remoteSessionStore, resolveSessionWriteDevices, sessionPendingWrites } from '@/session/remoteSessionStore';
+import { buildMobileSessionMessageDeepLink, parseSessionDeepLinkUrl, resolveSessionLinkDeviceId } from '@/session/sessionLinks';
+import { sessionMenuCopyLink } from '@/session/sessionMenu';
import type { InputProjection, PendingInteraction, RemoteMessage, RemoteSession } from '@/session/types';
import { setJankMetricsEnabled, takeJankMetrics } from '@/debug/jankMetrics';
@@ -5166,6 +5168,35 @@ describe('remoteSessionStore', () => {
expect(remoteSessionStore.getSessionDeviceId('s-stale')).toBe('stale-mac');
});
+ it('keeps links generated after re-link routed to the new device after archival removes the lookup', () => {
+ const routeDeviceId = 'old-mac';
+ remoteSessionStore.setDeviceIdentity([{ deviceId: routeDeviceId, name: 'Mac' }]);
+ remoteSessionStore.setDeviceSessions(routeDeviceId, 'Mac', [session('source')]);
+ const before = remoteSessionStore.getSessions()[0]!;
+ expect(parseSessionDeepLinkUrl(buildMobileSessionMessageDeepLink(
+ 'source', 'anchor', { session: before, deviceId: routeDeviceId },
+ ))?.deviceId).toBe(routeDeviceId);
+
+ remoteSessionStore.setDeviceIdentity([{ deviceId: 'new-mac', name: 'Mac' }]);
+ const current = remoteSessionStore.getSessions()[0]!;
+ expect(current.canonicalDeviceId).toBe('new-mac');
+ expect(current.deviceLinkDeviceId).toBe(routeDeviceId);
+ const links = [
+ buildMobileSessionMessageDeepLink('source', 'anchor', { session: current, deviceId: routeDeviceId }),
+ sessionMenuCopyLink(current),
+ ];
+ remoteSessionStore.applySessionPatch(routeDeviceId, 'source', { status: 'archived' });
+ expect(remoteSessionStore.getSessionDeviceId('source')).toBeUndefined();
+ for (const link of links) {
+ const target = parseSessionDeepLinkUrl(link)!;
+ expect(resolveSessionLinkDeviceId({
+ listedDeviceId: remoteSessionStore.getSessionDeviceId(target.sessionId),
+ linkedDeviceId: target.deviceId,
+ currentDeviceId: 'other-mac',
+ })).toBe('new-mac');
+ }
+ });
+
it('dedupes the same session id across stale/current shards, keeping the current shard copy', () => {
remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Lizi Mac' }]);
// 同一 session id 同时出现在 stale 与 current 两个 shard(re-link 后旧 shard 残留)。
diff --git a/apps/mobile/src/__tests__/sessionLinks.test.ts b/apps/mobile/src/__tests__/sessionLinks.test.ts
index a1b1e218cc..c6fd2f08cd 100644
--- a/apps/mobile/src/__tests__/sessionLinks.test.ts
+++ b/apps/mobile/src/__tests__/sessionLinks.test.ts
@@ -99,6 +99,17 @@ describe('session links', () => {
expect(buildMobileSessionMessageDeepLink('abc', 'c1', { deviceId: null })).toBe('cindy://session/abc?message=c1');
});
+ it.each([
+ [{ canonicalDeviceId: 'canonical', deviceLinkDeviceId: 'physical' }, 'canonical'],
+ [{ deviceLinkDeviceId: 'physical' }, 'physical'],
+ [null, 'route'],
+ [{ canonicalDeviceId: sharedTaskHostPeer('shared', 'host'), deviceLinkDeviceId: 'old' }, 'host'],
+ ] as const)('uses current session ownership before the retained route: %j', (session, expected) => {
+ const options = { session, deviceId: 'route' };
+ expect(parseSessionDeepLinkUrl(buildMobileSessionDeepLink('abc', options))?.deviceId).toBe(expected);
+ expect(parseSessionDeepLinkUrl(buildMobileSessionMessageDeepLink('abc', 'm1', options))?.deviceId).toBe(expected);
+ });
+
it('exports shared-task links with the physical host device instead of a local peer handle', () => {
const deviceId = sharedTaskHostPeer('shared-task', 'host-device');
expect(buildMobileSessionDeepLink('abc', { deviceId })).toBe(
diff --git a/apps/mobile/src/session/sessionLinks.ts b/apps/mobile/src/session/sessionLinks.ts
index c141cff1b9..d4ff69c081 100644
--- a/apps/mobile/src/session/sessionLinks.ts
+++ b/apps/mobile/src/session/sessionLinks.ts
@@ -1,5 +1,6 @@
import { parseSharedTaskPeer } from '@cindy/device-link';
import { BRAND_IDENTITY, allDeepLinkSchemes } from '@cindy/maker-shared/brand-identity';
+import type { RemoteSession } from '@/session/types';
// 深链双 scheme(身份单点派生):cindy 主 + xdt-maker 永久兼容(存量消息里的
// 老链接不能死);生成一律用主 scheme。与桌面端 shared/deepLinkSchemes 同源镜像。
@@ -27,6 +28,8 @@ export function isCindyDeepLinkUrl(url: string): boolean {
* 目标任务归档 / 未同步后,点开与引用仍能按它找到那台电脑,不必猜当前设备。
*/
export interface MobileSessionDeepLinkOptions {
+ /** 当前同步的任务信息优先于页面保留的设备路由。 */
+ session?: Pick<RemoteSession, 'canonicalDeviceId' | 'deviceLinkDeviceId'> | null;
deviceId?: string | null;
}
@@ -37,7 +40,10 @@ function sessionDeepLinkQuery(
const pairs: string[] = [];
if (messageClientId) pairs.push(`message=${encodeURIComponent(messageClientId)}`);
// 共享任务句柄只在当前客户端有效;跨端链接携带房主的真实电脑 ID。
- const deviceId = parseSharedTaskPeer(opts?.deviceId)?.deviceId ?? opts?.deviceId;
+ const sourceDeviceId = opts?.session?.canonicalDeviceId
+ ?? opts?.session?.deviceLinkDeviceId
+ ?? opts?.deviceId;
+ const deviceId = parseSharedTaskPeer(sourceDeviceId)?.deviceId ?? sourceDeviceId;
if (deviceId) pairs.push(`device=${encodeURIComponent(deviceId)}`);
return pairs.length > 0 ? `?${pairs.join('&')}` : '';
}
diff --git a/apps/mobile/src/session/sessionMenu.ts b/apps/mobile/src/session/sessionMenu.ts
index f84a9681b7..d5dd676162 100644
--- a/apps/mobile/src/session/sessionMenu.ts
+++ b/apps/mobile/src/session/sessionMenu.ts
@@ -165,9 +165,7 @@ export function removeSessionExtraDir(
export function sessionMenuCopyLink(
session: Pick<RemoteSession, 'id' | 'canonicalDeviceId' | 'deviceLinkDeviceId'>,
): string {
- return buildMobileSessionDeepLink(session.id, {
- deviceId: session.canonicalDeviceId ?? session.deviceLinkDeviceId,
- });
+ return buildMobileSessionDeepLink(session.id, { session });
}
/** Android 返回键 / 关闭手势的两段式收敛:info 先回 menu,menu 才关 sheet。 */
@@ -261,4 +259,3 @@ function lastPathPart(path: string): string {
function readPositiveNumber(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : null;
}
-