Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): serialize share markers and reject stale membership
DavidShen2ebd1eee31f5
diff --git a/apps/desktop/src/main/device-link/__tests__/providerShareCrossRegion.test.ts b/apps/desktop/src/main/device-link/__tests__/providerShareCrossRegion.test.tsindex d63b309747..88820fb896 100644--- a/apps/desktop/src/main/device-link/__tests__/providerShareCrossRegion.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/providerShareCrossRegion.test.ts@@ -3,6 +3,7 @@* 401 登出本账号;只有发过申请或加入过分享的账号才会联系对方区域;凭证只在内存里。*/import fs from 'node:fs';+import fsp from 'node:fs/promises';import os from 'node:os';import path from 'node:path';@@ -98,6 +99,12 @@ const share = {hostOnline: true, hostCapable: true,};+function deferred<T = void>() {+ let resolve!: (value: T | PromiseLike<T>) => void;+ const promise = new Promise<T>((done) => { resolve = done; });+ return { promise, resolve };+}+let dir: string;beforeEach(() => {dir = fs.mkdtempSync(path.join(os.tmpdir(), 'provider-share-x-'));@@ -110,6 +117,7 @@ beforeEach(() => {state.identityCards = [];});afterEach(() => {+ vi.restoreAllMocks();stopProviderShareGuest();stopProviderShareCrossRegion();__testing.setUserDataDir(null);@@ -117,6 +125,111 @@ afterEach(() => {});describe('cross-region provider share', () => {+ it('serializes marker read-modify-write across accounts and regions', async () => {+ const firstAccount = 'a'.repeat(64);+ const secondAccount = 'b'.repeat(64);+ await __testing.writeMarker(firstAccount, 'global', true);+ const entered = deferred();+ const release = deferred();+ const rename = fsp.rename.bind(fsp);+ vi.spyOn(fsp, 'rename').mockImplementationOnce(async (...args) => {+ entered.resolve();+ await release.promise;+ return rename(...args);+ });+ const first = __testing.writeMarker(firstAccount, 'global', false, true);+ await entered.promise;+ const second = __testing.writeMarker(secondAccount, 'cn', true);+ release.resolve();+ await Promise.all([first, second]);+ expect(__testing.readMarkers()).toEqual({ [secondAccount]: ['cn'] });+ expect(__testing.readMarkers('emptyAccounts')).toEqual({ [firstAccount]: ['global'] });+ });++ it('continues queued marker writes after a storage failure', async () => {+ const account = 'a'.repeat(64);+ vi.spyOn(fsp, 'rename').mockRejectedValueOnce(new Error('temporary storage failure'));+ const failed = __testing.writeMarker(account, 'global', false, true);+ const next = __testing.writeMarker(account, 'global', true);+ await expect(failed).rejects.toThrow('temporary storage failure');+ await next;+ expect(__testing.readMarkers()).toEqual({ [account]: ['global'] });+ expect(__testing.readMarkers('emptyAccounts')).toEqual({});+ });++ it.each(['response', 'write'] as const)('keeps a new application when the old empty %s completes late', async (phase) => {+ state.responses.set('/requests', { requestId: 'r1', status: 'pending', pairingCode: '0042', expiresAt: new Date().toISOString(), requestToken: TOKEN });+ await crossRegionSendRequest('global', TOKEN);+ const active = __testing.readMarkers();+ const oldResponse = deferred<unknown>();+ const freshResponse = deferred<unknown>();+ const writeEntered = deferred();+ const releaseWrite = deferred();+ state.responses.set('/session', oldResponse.promise);+ const changed = vi.fn();+ startProviderShareCrossRegion({ changed }, { userDataDir: dir });+ const oldRefresh = refreshProviderShareCrossRegion('global');+ await vi.waitFor(() => expect(state.calls.some((call) => call.path.endsWith('/session'))).toBe(true));+ if (phase === 'write') {+ const writeFile = fsp.writeFile.bind(fsp);+ vi.spyOn(fsp, 'writeFile').mockImplementationOnce(async (...args) => {+ await writeFile(...args);+ writeEntered.resolve();+ await releaseWrite.promise;+ });+ oldResponse.resolve({ credential: null, expiresAt: null, shares: [] });+ await writeEntered.promise;+ }+ state.responses.set('/session', freshResponse.promise);+ const application = crossRegionSendRequest('global', TOKEN);+ // Request parsing only crosses microtasks before queuing its disk write.+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(getConfirmedCrossRegionReceivedRegions()).toEqual([]);+ if (phase === 'response') await application;+ oldResponse.resolve({ credential: null, expiresAt: null, shares: [] });+ releaseWrite.resolve();+ await Promise.all([application, oldRefresh]);+ expect(__testing.readMarkers()).toEqual(active);+ expect(__testing.readMarkers('emptyAccounts')).toEqual({});+ expect(getConfirmedCrossRegionReceivedRegions()).toEqual([]);+ expect(changed).not.toHaveBeenCalled();+ // A pending application alone must not trigger another empty list lookup.+ expect(state.calls.filter((call) => call.path.endsWith('/session'))).toHaveLength(1);+ freshResponse.resolve({ credential: CREDENTIAL, expiresAt: new Date(Date.now() + 86_400_000).toISOString(), shares: [share] });+ await refreshProviderShareCrossRegion('global');+ expect(getCrossRegionReceived()).toEqual([share]);+ expect(changed).toHaveBeenCalledOnce();+ expect(__testing.readMarkers()).toEqual(active);+ stopProviderShareCrossRegion();+ startProviderShareCrossRegion({ changed: vi.fn() }, { userDataDir: dir });+ await refreshProviderShareCrossRegion('global');+ expect(getCrossRegionReceived()).toEqual([share]);+ });++ it('keeps existing membership confirmed and refreshes approval behind an obsolete request', async () => {+ state.responses.set('/requests', { requestId: 'r1', status: 'pending', pairingCode: '0042', expiresAt: new Date().toISOString(), requestToken: TOKEN });+ await crossRegionSendRequest('global', TOKEN);+ state.responses.set('/session', { credential: CREDENTIAL, expiresAt: new Date(Date.now() + 86_400_000).toISOString(), shares: [share] });+ startProviderShareCrossRegion({ changed: vi.fn() }, { userDataDir: dir });+ await refreshProviderShareCrossRegion('global');+ const staleResponse = deferred<unknown>();+ state.responses.set('/session', staleResponse.promise);+ const pending = refreshProviderShareCrossRegion('global');+ await vi.waitFor(() => expect(state.calls.filter((call) => call.path.endsWith('/session'))).toHaveLength(2));+ await crossRegionSendRequest('global', TOKEN);+ expect(getConfirmedCrossRegionReceivedRegions()).toEqual(['global']);+ const newShare = { ...share, shareId: 'new-share', memberId: 'new-member' };+ state.responses.set('/session', { credential: CREDENTIAL, expiresAt: new Date(Date.now() + 86_400_000).toISOString(), shares: [share, newShare] });+ const approved = refreshProviderShareCrossRegion('global');+ const anotherWindow = refreshProviderShareCrossRegion('global');+ staleResponse.resolve({ credential: null, expiresAt: null, shares: [] });+ await Promise.all([pending, approved, anotherWindow]);+ expect(state.calls.filter((call) => call.path.endsWith('/session'))).toHaveLength(3);+ expect(getCrossRegionReceived()).toEqual([share, newShare]);+ expect(getConfirmedCrossRegionReceivedRegions()).toEqual(['global']);+ expect(__testing.readMarkers('emptyAccounts')).toEqual({});+ });+it('does not send identity cards for forged favorite hints or another account history', async () => {const probe = async () => {startProviderShareGuest({ changed: vi.fn(), settled: vi.fn() }, { userDataDir: dir });diff --git a/apps/desktop/src/main/device-link/providerShareCrossRegion.ts b/apps/desktop/src/main/device-link/providerShareCrossRegion.tsindex 88f2091fb6..e4a74f65b6 100644--- a/apps/desktop/src/main/device-link/providerShareCrossRegion.ts+++ b/apps/desktop/src/main/device-link/providerShareCrossRegion.ts@@ -66,10 +66,12 @@ interface RegionState {shares: ProviderShareReceived[];client: DeviceLinkClient | null;refreshing: Promise<void> | null;+ refreshingRevision: number;timer: ReturnType<typeof setTimeout> | null;/** 连续失败次数,用于退避重试(网络或对方区域暂时不可用时不让分享整段消失)。 */failures: number;loaded: boolean;+ requestRevision: number;}interface Runtime {@@ -81,6 +83,7 @@ interface Runtime {let runtime: Runtime | null = null;let userDataDir: string | null = null;+let markerWrites: Promise<void> = Promise.resolve();function peerRegion(): Region {return getActiveAuthRealm() === 'cn' ? 'global' : 'cn';@@ -101,8 +104,7 @@ function markerPath(): string | null {return userDataDir ? path.join(userDataDir, 'remote-agent', MARKER_FILE) : null;}-function readMarkers(field: 'accounts' | 'emptyAccounts' = 'accounts'): Record<string, Region[]> {- const file = markerPath();+function readMarkers(field: 'accounts' | 'emptyAccounts' = 'accounts', file = markerPath()): Record<string, Region[]> {if (!file) return {};try {const parsed = JSON.parse(fs.readFileSync(file, 'utf8')) as { version?: unknown; accounts?: unknown; emptyAccounts?: unknown };@@ -119,12 +121,20 @@ function readMarkers(field: 'accounts' | 'emptyAccounts' = 'accounts'): Record<s}}-async function writeMarker(account: string, region: Region, present: boolean, confirmedEmpty = false): Promise<void> {+function writeMarker(account: string, region: Region, present: boolean, confirmedEmpty = false, isCurrent = () => true): Promise<void> {const file = markerPath();- if (!file) return;- const markers = readMarkers();+ if (!file) return Promise.resolve();+ // 整份文件共享一条写链;读取也必须在队列内,失败不能阻塞后续账号/区域的更新。+ const write = markerWrites.then(() => writeMarkerFile(file, account, region, present, confirmedEmpty, isCurrent));+ markerWrites = write.catch(() => undefined);+ return write;+}++async function writeMarkerFile(file: string, account: string, region: Region, present: boolean, confirmedEmpty: boolean, isCurrent: () => boolean): Promise<void> {+ if (!isCurrent()) return;+ const markers = readMarkers('accounts', file);// 服务端确认的空名单只供本地收藏清理,绝不授予跨区联网能力。新申请使旧结论失效。- const emptyAccounts = readMarkers('emptyAccounts');+ const emptyAccounts = readMarkers('emptyAccounts', file);const emptyRegions = new Set(emptyAccounts[account] ?? []);if (confirmedEmpty && !present) emptyRegions.add(region);else emptyRegions.delete(region);@@ -139,7 +149,7 @@ async function writeMarker(account: string, region: Region, present: boolean, coawait fsp.mkdir(path.dirname(file), { recursive: true });try {await fsp.writeFile(tmp, JSON.stringify({ version: 1, accounts: markers, emptyAccounts }), { encoding: 'utf8', mode: 0o600 });- await fsp.rename(tmp, file);+ if (isCurrent()) await fsp.rename(tmp, file);} finally {await fsp.rm(tmp, { force: true }).catch(() => undefined);}@@ -220,6 +230,15 @@ export async function crossRegionSendRequest(region: Region, invitation: string)const identityCard = await fetchIdentityCard('share-request', sha256Hex(invitation));const value = await peerRequest(base, '/requests', { invitation, identityCard });const result = { state: providerShareParsers.requestState(value), requestToken: providerShareParsers.secret(value.requestToken) };+ const rt = runtime;+ if (rt && current(rt) && rt.account === account) {+ const state = rt.regions.get(region);+ if (state) {+ // 先使在途旧名单失效,再排队落盘;旧空名单不能清理新申请对应的收藏。+ state.requestRevision += 1;+ if (state.shares.length === 0) state.loaded = false;+ }+ }// 记下「这个账号在对方区域有往来」,之后才会去对方区域拉分享。await writeMarker(account, region, true).catch((error) => log.warn('cross-region marker write failed', { error: String(error) }));return result;@@ -281,18 +300,27 @@ function scheduleRefresh(rt: Runtime, state: RegionState): void {}function initialRegionState(region: Region, loaded = false): RegionState {- return { region, apiBase: '', credential: null, expiresAt: 0, shares: [], client: null, refreshing: null, timer: null, failures: 0, loaded };+ return { region, apiBase: '', credential: null, expiresAt: 0, shares: [], client: null, refreshing: null, refreshingRevision: 0, timer: null, failures: 0, loaded, requestRevision: 0 };}/** 用新名片换取这台电脑在对方区域的分享列表与连接凭证。 */function refreshRegion(rt: Runtime, region: Region): Promise<void> {let state = rt.regions.get(region);- if (state?.refreshing) return state.refreshing;+ if (state?.refreshing) {+ // 新申请获批后的刷新不能仅等待旧一轮;排在旧请求后补读,不提前查询待审批申请。+ return state.refreshingRevision === state.requestRevision ? state.refreshing+ : state.refreshing.then(() => current(rt) ? refreshRegion(rt, region) : undefined);+ }if (!state) {state = initialRegionState(region);rt.regions.set(region, state);}const target = state;+ if (target.timer) clearTimeout(target.timer);+ target.timer = null;+ const revision = target.requestRevision;+ target.refreshingRevision = revision;+ const isCurrent = () => current(rt) && target.requestRevision === revision;const run = (async () => {try {const deviceId = getDeviceId();@@ -305,7 +333,7 @@ function refreshRegion(rt: Runtime, region: Region): Promise<void> {deviceName: deviceName(),platform: process.platform,});- if (!current(rt)) return;+ if (!isCurrent()) return;const shares = providerShareParsers.receivedList(value);const credential = value.credential === null || value.credential === undefined ? null : providerShareParsers.secret(value.credential);const expiresAt = credential ? Date.parse(providerShareParsers.timestamp(value.expiresAt)) : 0;@@ -320,16 +348,16 @@ function refreshRegion(rt: Runtime, region: Region): Promise<void> {state.client?.stop();state.client = null;}- await writeMarker(rt.account, region, shares.length > 0 && !!credential, shares.length === 0).catch((error) =>+ await writeMarker(rt.account, region, shares.length > 0 && !!credential, shares.length === 0, isCurrent).catch((error) =>log.warn('cross-region marker write failed', { error: String(error) }));- if (!current(rt)) return;+ if (!isCurrent()) return;if (shares.length > 0 && credential && !state.client) {state.client = createClient(rt, state);}scheduleRefresh(rt, state);rt.events.changed();} catch (error) {- if (current(rt)) {+ if (isCurrent()) {log.warn('cross-region session refresh failed', { region, error: error instanceof Error ? error.message : String(error) });// 退避重试:1 → 2 → 4 … 最多 30 分钟;关闭跨区开关等确定性错误同样按退避慢慢重试。target.failures += 1;@@ -339,6 +367,7 @@ function refreshRegion(rt: Runtime, region: Region): Promise<void> {}} finally {target.refreshing = null;+ if (current(rt) && target.requestRevision !== revision) scheduleRefresh(rt, target);}})();target.refreshing = run;