Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(provider-groups): reuse capability reader across detail refreshes
MagicLizia4df240aa4b5
diff --git a/apps/desktop/src/main/provider-group/__tests__/router.test.ts b/apps/desktop/src/main/provider-group/__tests__/router.test.tsindex 13f42db39c..8089eaf71a 100644--- a/apps/desktop/src/main/provider-group/__tests__/router.test.ts+++ b/apps/desktop/src/main/provider-group/__tests__/router.test.ts@@ -4,13 +4,14 @@*/import type { ProviderView } from '@cindy/model-providers';import type { RemoteAgentCaps } from '@cindy/device-link';-import { describe, expect, it, vi } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import type { ProviderGroupConfig, ProviderGroupMember } from '../../../shared/providerGroup';import type { ProviderGroupBinding } from '../bindings';import type { DeviceLinkDeviceView } from '../../../shared/deviceLinkIpc';import { createProviderGroupDirectory, type ProviderGroupDirectory, type ResolvedProviderGroupMember } from '../directory';import { createProviderGroupRouter, PROVIDER_GROUP_PICK_QUICK_MS, type ProviderGroupRouterDeps } from '../router';+import { SHARED_GROUP_STATUS_CAPS_CACHE_MS, SHARED_GROUP_STATUS_TIMEOUT_MS } from '../sharedStatus';const MODEL = 'claude-opus-5-5';@@ -68,6 +69,126 @@ function harness(reported: Record<string, number>, bindings: Record<string, Prov});}+describe('provider group details capability reader lifetime', () => {+ const devices = Array.from({ length: 5 }, (_, index) => member(`device:${index}:anthropic`, 'device', `device-${index}`, 1));+ const supportedCaps = { virtualWorkspace: true, guestRelay: true } as RemoteAgentCaps;+ const olderCaps = { virtualWorkspace: true } as RemoteAgentCaps;+ const detailsOptions = { includeGuestSupport: true };++ beforeEach(() => vi.useFakeTimers());+ afterEach(() => vi.useRealTimers());++ function detailsRouter(readCaps: NonNullable<ProviderGroupRouterDeps['readCaps']>) {+ return harness({}, {}, [], {+ readGroup: () => ({ strategy: 'least', autoSwitch: true, members: devices }), readCaps,+ });+ }++ it('keeps four physical probes across overlapping details and the next refresh', async () => {+ const readCaps = vi.fn<NonNullable<ProviderGroupRouterDeps['readCaps']>>(() => new Promise(() => undefined));+ const router = detailsRouter(readCaps);+ const first = router.view('anthropic', detailsOptions);+ const overlapping = router.view('anthropic', detailsOptions);+ await vi.advanceTimersByTimeAsync(0);+ expect(readCaps).toHaveBeenCalledTimes(4);+ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_TIMEOUT_MS);+ for (const details of await Promise.all([first, overlapping])) {+ expect(details.members.map((entry) => entry.guestSupport)).toEqual(Array(5).fill('unknown'));+ }++ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_CAPS_CACHE_MS - SHARED_GROUP_STATUS_TIMEOUT_MS);+ const refreshed = router.view('anthropic', detailsOptions);+ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_TIMEOUT_MS);+ expect((await refreshed).members.map((entry) => entry.guestSupport)).toEqual(Array(5).fill('unknown'));+ expect(readCaps).toHaveBeenCalledTimes(4);+ });++ it('uses late results on the next refresh so a fifth computer can be probed', async () => {+ const readCaps = vi.fn<NonNullable<ProviderGroupRouterDeps['readCaps']>>((current) => current.agentDeviceId === devices[4].agentDeviceId+ ? Promise.resolve(supportedCaps)+ : new Promise((resolve) => setTimeout(() => resolve(supportedCaps), SHARED_GROUP_STATUS_TIMEOUT_MS + 1_000)));+ const router = detailsRouter(readCaps);+ const first = router.view('anthropic', detailsOptions);+ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_TIMEOUT_MS);+ expect((await first).members.map((entry) => entry.guestSupport)).toEqual(Array(5).fill('unknown'));+ expect(readCaps).toHaveBeenCalledTimes(4);++ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_CAPS_CACHE_MS - SHARED_GROUP_STATUS_TIMEOUT_MS);+ const refreshed = router.view('anthropic', detailsOptions);+ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_TIMEOUT_MS);+ expect((await refreshed).members.map((entry) => entry.guestSupport)).toEqual(Array(5).fill('supported'));+ expect(readCaps).toHaveBeenCalledTimes(5);+ expect(readCaps).toHaveBeenLastCalledWith(devices[4]);+ });++ it('caches capabilities for 15 seconds but still reads the current running state', async () => {+ const device = devices[0];+ const reported = { [device.key]: 0 };+ const readCaps = vi.fn<NonNullable<ProviderGroupRouterDeps['readCaps']>>().mockResolvedValue(supportedCaps);+ const router = harness(reported, {}, [], {+ readGroup: () => ({ strategy: 'least', autoSwitch: true, members: [device] }), readCaps,+ });+ expect((await router.view('anthropic', detailsOptions)).members[0]).toMatchObject({+ guestSupport: 'supported', state: 'available', running: 0,+ });+ reported[device.key] = 1;+ readCaps.mockResolvedValue(olderCaps);+ await vi.advanceTimersByTimeAsync(SHARED_GROUP_STATUS_CAPS_CACHE_MS - 1);+ expect((await router.view('anthropic', detailsOptions)).members[0]).toMatchObject({+ guestSupport: 'supported', state: 'full', running: 1,+ });+ expect(readCaps).toHaveBeenCalledTimes(1);++ await vi.advanceTimersByTimeAsync(1);+ expect((await router.view('anthropic', detailsOptions)).members[0]).toMatchObject({+ guestSupport: 'needs-update', state: 'full', running: 1,+ });+ expect(readCaps).toHaveBeenCalledTimes(2);+ });++ it('shares an in-flight device probe across different groups in the same router', async () => {+ const device = devices[0];+ const otherProvider = { ...device, key: 'device:0:openai', providerId: 'openai' };+ const readCaps = vi.fn<NonNullable<ProviderGroupRouterDeps['readCaps']>>(() =>+ new Promise((resolve) => setTimeout(() => resolve(supportedCaps), 1)));+ const router = harness({}, {}, [], {+ readGroup: (providerId) => ({+ strategy: 'least', autoSwitch: true, members: [providerId === 'anthropic' ? device : otherProvider],+ }),+ readCaps,+ });+ const first = router.view('anthropic', detailsOptions);+ const other = router.view('openai', detailsOptions);+ await vi.advanceTimersByTimeAsync(0);+ expect(readCaps).toHaveBeenCalledTimes(1);+ await vi.advanceTimersByTimeAsync(1);+ for (const details of await Promise.all([first, other])) {+ expect(details.members[0].guestSupport).toBe('supported');+ }+ await router.view('openai', detailsOptions);+ expect(readCaps).toHaveBeenCalledTimes(1);+ });++ it('isolates in-flight probes and caches between router account runtimes', async () => {+ let finishOldProbe!: (caps: RemoteAgentCaps) => void;+ const readCaps = vi.fn<NonNullable<ProviderGroupRouterDeps['readCaps']>>()+ .mockImplementationOnce(() => new Promise((resolve) => { finishOldProbe = resolve; }))+ .mockResolvedValue(olderCaps);+ const overrides = {+ readGroup: (): ProviderGroupConfig => ({ strategy: 'least', autoSwitch: true, members: [devices[0]] }), readCaps,+ };+ const oldRouter = harness({}, {}, [], overrides);+ const first = oldRouter.view('anthropic', detailsOptions);+ await vi.advanceTimersByTimeAsync(0);+ const nextRouter = harness({}, {}, [], overrides);+ expect((await nextRouter.view('anthropic', detailsOptions)).members[0].guestSupport).toBe('needs-update');+ finishOldProbe(supportedCaps);+ expect((await first).members[0].guestSupport).toBe('supported');+ expect((await oldRouter.view('anthropic', detailsOptions)).members[0].guestSupport).toBe('supported');+ expect(readCaps).toHaveBeenCalledTimes(2);+ });+});+describe('provider group running count', () => {it('only probes sharing support for details and keeps an older computer usable by its owner', async () => {const device = member('device:old:anthropic', 'device', 'old');diff --git a/apps/desktop/src/main/provider-group/router.ts b/apps/desktop/src/main/provider-group/router.tsindex 2101b18fc3..264048f7e0 100644--- a/apps/desktop/src/main/provider-group/router.ts+++ b/apps/desktop/src/main/provider-group/router.ts@@ -107,6 +107,7 @@ function coolKey(providerId: string, memberKey: string): string {}export function createProviderGroupRouter(deps: ProviderGroupRouterDeps): ProviderGroupRouter {+ const guestSupportReader = deps.readCaps ? createGuestSupportReader(deps.readCaps) : null;const cooling = new Map<string, number>();try {for (const [providerId, members] of Object.entries(deps.coolingStore?.read() ?? {})) {@@ -241,7 +242,7 @@ export function createProviderGroupRouter(deps: ProviderGroupRouterDeps): Providconst config = deps.readGroup(providerId);if (!config) return { providerId, config: null, members: [] };const resolved = await deps.directory.resolveMembers(providerId, config);- const readSupport = options?.includeGuestSupport && deps.readCaps ? createGuestSupportReader(deps.readCaps) : null;+ const readSupport = options?.includeGuestSupport ? guestSupportReader : null;return {providerId,config,diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.mdindex 294e92218b..54635beacc 100644--- a/docs/dev-rules/protocol-compatibility.md+++ b/docs/dev-rules/protocol-compatibility.md@@ -980,6 +980,9 @@ G 重新打开时换一台。G 与 O 之间新增四项可选内容,O 与 M- 同账号组详情的 view 请求可选带 includeGuestSupport: true,才按现有远程 Agent 能力查询核对能否接收分享任务。普通状态与任务分配不增加能力探测,不修改实际分配、冷却或权限。单台查询最多等 3 秒,失败或超时为未知。+- 能力读取器跟随当前账号的组 router 运行期,跨详情刷新、跨组按电脑复用在途查询与 15 秒短缓存,最多 4 个物理在途槽。+ 等待超时不释放仍在途的槽,未发出的排队查询到期取消;迟到结果供下一次详情读取使用,避免后续电脑反复排队超时。+ 不跨账号复用,不缓存忙闲等组状态,不改变底层 device-link 超时、重连或 wire 格式。回归见 `router.test.ts`。- 成员状态可选携带 appVersion(不超过 64 字符的应用版本)与 guestSupport(supported / needs-update / unknown)。本机版本由应用提供,同账号设备版本复用设备目录;分享来源没有版本则省略。能力按真实声明判断,不按版本号猜测。- 新端白名单解析;旧组所在电脑忽略选项、未带字段时不推断版本或兼容状态,未知信息放在悬停说明里,旧控制端忽略新增字段。diff --git a/docs/product-rules/provider-groups.md b/docs/product-rules/provider-groups.mdindex 570178af22..22751d2848 100644--- a/docs/product-rules/provider-groups.md+++ b/docs/product-rules/provider-groups.md@@ -449,7 +449,9 @@- 详情按需核对能否接收分享任务;仅确定缺少必要能力时在名称行显示「需要更新」,完整原因在悬停说明中,不覆盖主人自己的空闲、运行中等状态。这里不是检查有没有更新发布,也不意味着主人不能使用该电脑。读取失败或旧组所在电脑未提供能力时,只在悬停说明中显示兼容性未知,不逐行重复常驻。- - 只读能力探测每台最多等待 3 秒,同一次请求按电脑合并;普通状态查询与任务分配不增加探测,不修改冷却与分配。+ - 只读能力探测每台最多等待 3 秒,在当前账号的组运行期内跨详情刷新、跨组按电脑复用查询,最多 4 个物理在途槽。+ 等待超时不释放仍在途的槽,未发出的排队查询到期取消;已结算能力短存 15 秒,迟到结果供下一次详情读取使用。+ 不跨账号复用能力;忙闲等状态仍每次现读,普通状态查询与任务分配不增加探测,不修改冷却与分配。- **组内电脑一行的结构对所有来源一致**,右侧才能真正成列:设备图标 → 名称、版本与来源 → 状态行 →并发上限(按权重时多一个权重)→ 参与分配开关 → 行内 `···` 菜单。三条由此固定:- **参与分配只有一种控件。** `paused` 是同一个布尔值,本机与远程都用开关表达,不按来源分叉成