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(provider-group): 在模型选择里换到组那一项时,由组选一台能用的电脑

Lizi247bf3c51375
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
index 2428836885..3adafbcb49 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
@@ -1444,3 +1444,72 @@ describe('远程 Agent:选模型时换 Agent 所在电脑', () => {
});
});
});
+
+describe('换到供应商组那一项:由组选一台能用的电脑(provider-groups.md §6)', () => {
+ function groupHarness(resolve: MakerSessionAgentSwitchHandlerDeps['resolveProviderGroupTarget']) {
+ const pending = createPendingAgentSwitchRegistry();
+ const row = makeRow({ agentDeviceId: null, providerId: 'xd' });
+ const resolveProviderGroupTarget = vi.fn(resolve);
+ const { deps } = makeDeps({
+ pendingSwitches: pending,
+ getSessionRow: vi.fn(async () => row),
+ assertAgentDeviceRouteUsable: vi.fn(async () => undefined),
+ resolveProviderGroupTarget,
+ });
+ return { deps, pending, resolveProviderGroupTarget };
+ }
+ const toGroup = {
+ sessionId: 's1',
+ targetAgentKind: 'claude-code',
+ model: 'claude-opus-5-5',
+ providerId: 'anthropic',
+ agentDeviceId: 'group-host',
+ };
+
+ it('登记时不选,发送时按组此刻的状态选一台,换到那台', async () => {
+ const h = groupHarness(async () => ({ agentDeviceId: 'office-pc', providerId: 'anthropic-2' }));
+ await performSessionAgentSwitch(h.deps, toGroup);
+ expect(h.resolveProviderGroupTarget).not.toHaveBeenCalled();
+ expect(h.pending.get('s1')).toMatchObject({ targetAgentDeviceId: 'group-host' });
+
+ await applyPendingAgentSwitchIfIdle(h.deps, 's1');
+ expect(h.resolveProviderGroupTarget).toHaveBeenCalledWith('s1', {
+ agentKind: 'claude-code',
+ model: 'claude-opus-5-5',
+ providerId: 'anthropic',
+ agentDeviceId: 'group-host',
+ current: { agentDeviceId: null, providerId: 'xd' },
+ });
+ expect(h.deps.applyAgentSwitchToDb).toHaveBeenCalledWith('s1', expect.objectContaining({
+ agentDeviceId: 'office-pc',
+ providerId: 'anthropic-2',
+ }));
+ expect(h.pending.get('s1')).toBeUndefined();
+ });
+
+ it('不是组那一项时照用户选的位置换', async () => {
+ const h = groupHarness(async () => null);
+ await performSessionAgentSwitch(h.deps, toGroup);
+ await applyPendingAgentSwitchIfIdle(h.deps, 's1');
+ expect(h.deps.applyAgentSwitchToDb).toHaveBeenCalledWith('s1', expect.objectContaining({
+ agentDeviceId: 'group-host',
+ providerId: 'anthropic',
+ }));
+ });
+
+ it('组里都不能用:不换、不发,意图留着', async () => {
+ const h = groupHarness(async () => {
+ throw new Error('[REMOTE_AGENT_GROUP_UNAVAILABLE] no computer in the provider group can run this task right now');
+ });
+ await performSessionAgentSwitch(h.deps, toGroup);
+ await expect(applyPendingAgentSwitchIfIdle(h.deps, 's1')).rejects.toThrow('REMOTE_AGENT_GROUP_UNAVAILABLE');
+ expect(h.deps.applyAgentSwitchToDb).not.toHaveBeenCalled();
+ expect(h.pending.get('s1')).toBeDefined();
+ });
+
+ it('供应商组自己换电脑时不再经组选一次', async () => {
+ const h = groupHarness(async () => ({ agentDeviceId: 'office-pc', providerId: 'anthropic-2' }));
+ await performSessionAgentSwitch(h.deps, { ...toGroup, applyNow: true, providerGroupRelocation: true });
+ expect(h.resolveProviderGroupTarget).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index e699551054..ed7a9df240 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -9536,6 +9536,9 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
});
const agentSwitchDeps: MakerSessionAgentSwitchHandlerDeps = {
+ // 换到供应商组那一项时由组选一台能用的电脑(provider-groups.md §6)。
+ resolveProviderGroupTarget: (sessionId, target) =>
+ providerGroupService ? providerGroupService.resolveSwitchTarget(sessionId, target) : Promise.resolve(null),
withSessionLock: withSendToSessionLock,
// 停用轴边界裁决:目标路由被停用 → 抛错;隐式默认落点被停用 → 返回启用替代来源。
// Agent 在另一台电脑运行的任务:目标模型属于那台的目录,由那台的 Agent 启动时裁决(选择时另
diff --git a/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts b/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
index 36d3ea07c4..96544341fb 100644
--- a/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
+++ b/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
@@ -127,6 +127,23 @@ export interface AgentSwitchSessionRow {
const AGENT_DEVICE_ID_PATTERN = /^[A-Za-z0-9_.:-]{1,128}$/;
export interface MakerSessionAgentSwitchHandlerDeps {
+ /**
+ * 任务要换到供应商组那一项(组所在电脑上那个建了组的供应商)时,由组按它的状态选一台能用的电脑
+ * (docs/product-rules/provider-groups.md §6):返回选中那台的位置;不是组那一项或组读不到返回 null(照用户选的位置);
+ * 组里都不能用时抛错。只在真正提交(applyNow)、要换电脑时调用。缺省 = 不经组选。
+ */
+ resolveProviderGroupTarget?(
+ sessionId: string,
+ target: {
+ agentKind: 'claude-code' | 'codex' | 'pi';
+ model: string;
+ providerId: string | null;
+ /** 用户选的位置(null = 任务所在电脑)。 */
+ agentDeviceId: string | null;
+ /** 任务此刻的位置。 */
+ current: { agentDeviceId: string | null; providerId: string | null };
+ },
+ ): Promise<{ agentDeviceId: string | null; providerId: string | null } | null>;
/** Same-engine choices use SET_MODEL validation; only the send boundary applies them. Caller owns the session lock. */
selectSameAgentModel?(
sessionId: string,
@@ -475,7 +492,7 @@ export async function performSessionAgentSwitch(
) {
throwIpcError('INVALID_PARAMS', 'agentDeviceId must be a device id or null');
}
- const requestedAgentDeviceId = rawAgentDeviceId as string | null | undefined;
+ let requestedAgentDeviceId = rawAgentDeviceId as string | null | undefined;
// 必须在第一个 await 前快照:跨窗口 / 跨设备写入即使 set→clear 回到同值,修订号也会变化。
const pendingRevisionAtStart = deps.pendingSwitches?.revision?.(sessionId);
let normalizedProviderId =
@@ -529,8 +546,30 @@ export async function performSessionAgentSwitch(
const fromDbKind: DbAgentKind = normalizeDbAgentKind(row.agentKind);
const toDbKind: DbAgentKind = makerToDbAgentKind(targetAgentKind);
const currentAgentDeviceId = row.agentDeviceId ?? null;
- const targetAgentDeviceId =
+ let targetAgentDeviceId =
requestedAgentDeviceId === undefined ? currentAgentDeviceId : requestedAgentDeviceId;
+ // 换到供应商组那一项、要换电脑:交接反正要做,由组按它此刻的状态选一台能用的电脑,不直接落到组所在电脑本身
+ // (docs/product-rules/provider-groups.md §6,2026-10-11 用户反馈)。登记意图时不选,真正提交时按那一刻的状态选。
+ if (
+ params.applyNow === true &&
+ params.providerGroupRelocation !== true &&
+ targetAgentDeviceId !== currentAgentDeviceId &&
+ deps.resolveProviderGroupTarget
+ ) {
+ const chosen = await deps.resolveProviderGroupTarget(sessionId, {
+ agentKind: targetAgentKind,
+ model,
+ providerId: typeof normalizedProviderId === 'string' ? normalizedProviderId : null,
+ agentDeviceId: targetAgentDeviceId,
+ current: { agentDeviceId: currentAgentDeviceId, providerId: row.providerId ?? null },
+ });
+ throwIfAgentSwitchAborted(signal);
+ if (chosen) {
+ targetAgentDeviceId = chosen.agentDeviceId;
+ requestedAgentDeviceId = chosen.agentDeviceId;
+ normalizedProviderId = chosen.providerId;
+ }
+ }
const agentDeviceChanges = targetAgentDeviceId !== currentAgentDeviceId;
// 位置不变的强制换电脑(只认内部 applyNow 调用,且 Agent 在另一台电脑上):交接、提交、分隔条都按换电脑处理。
const forcedRelocation =
diff --git a/apps/desktop/src/main/provider-group/__tests__/remoteGroup.test.ts b/apps/desktop/src/main/provider-group/__tests__/remoteGroup.test.ts
index 75c7122d25..06c03b9e09 100644
--- a/apps/desktop/src/main/provider-group/__tests__/remoteGroup.test.ts
+++ b/apps/desktop/src/main/provider-group/__tests__/remoteGroup.test.ts
@@ -592,3 +592,34 @@ describe('a group on another computer whose state shows a computer cannot be use
);
});
});
+
+describe('moving a task into a group on another computer from the model list (2026-10-11)', () => {
+ const CURRENT = { agentDeviceId: 'elsewhere', providerId: 'openai' };
+ const target = { agentKind: 'claude-code' as const, model: MODEL, providerId: 'anthropic', agentDeviceId: OWNER, current: CURRENT };
+
+ it('asks the group computer which computer to use instead of going to the group computer itself', async () => {
+ const h = harness({ picks: [STUDIO] });
+ const route = await h.service.resolveSwitchTarget('s1', target);
+ expect(h.remote.pick).toHaveBeenCalledWith(OWNER, expect.objectContaining({ providerId: 'anthropic', exclude: [] }));
+ expect(route).toEqual({ agentDeviceId: 'studio', providerId: 'anthropic-1a2b3c4d' });
+ expect(h.bindings.get('s1')).toMatchObject({ memberKey: STUDIO.key, groupDeviceId: OWNER });
+
+ // 组选中的是这台电脑自己:在这台运行。
+ const self = harness({ picks: [SELF] });
+ expect(await self.service.resolveSwitchTarget('s1', target)).toEqual({ agentDeviceId: null, providerId: 'anthropic' });
+ });
+
+ it('says at once when no computer in the group can run it', async () => {
+ const h = harness({ picks: ['unavailable'] });
+ await expect(h.service.resolveSwitchTarget('s1', target)).rejects.toThrow(PROVIDER_GROUP_UNAVAILABLE_ERROR);
+ expect(h.bindings.size).toBe(0);
+ });
+
+ it('keeps the chosen place when it is not a group or the group computer cannot be read', async () => {
+ expect(await harness({ config: null }).service.resolveSwitchTarget('s1', target)).toBeNull();
+ expect(await harness({ config: undefined }).service.resolveSwitchTarget('s1', target)).toBeNull();
+ expect(await harness({ picks: ['none'] }).service.resolveSwitchTarget('s1', target)).toBeNull();
+ // 分享来的供应商不是同账号的组。
+ expect(await harness().service.resolveSwitchTarget('s1', { ...target, agentDeviceId: 'share:s9' })).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/main/provider-group/__tests__/service.test.ts b/apps/desktop/src/main/provider-group/__tests__/service.test.ts
index 436f2962e2..1350c5fd44 100644
--- a/apps/desktop/src/main/provider-group/__tests__/service.test.ts
+++ b/apps/desktop/src/main/provider-group/__tests__/service.test.ts
@@ -1310,3 +1310,31 @@ describe('using the state the group keeps (2026-10-11)', () => {
expect(h.router.coolingUntil('anthropic', MINI.key)).toBeNull();
});
});
+
+describe('moving a task into a group on this computer from the model list (2026-10-11)', () => {
+ it('picks a computer by the group state, skipping one that is cooling down', async () => {
+ const h = harness({ members: [LOCAL, MINI, STUDIO], row: { agentDeviceId: 'elsewhere', providerId: 'openai', sdkSessionId: 'native' } });
+ h.router.markCooling('anthropic', 'local', 2_000_000);
+ const route = await h.service.resolveSwitchTarget('s1', {
+ agentKind: 'claude-code',
+ model: MODEL,
+ providerId: 'anthropic',
+ agentDeviceId: null,
+ current: { agentDeviceId: 'elsewhere', providerId: 'openai' },
+ });
+ expect(route).toEqual({ agentDeviceId: 'mini', providerId: MINI.providerId });
+ expect(h.bindings.get('s1')).toMatchObject({ providerId: 'anthropic', memberKey: MINI.key });
+ });
+
+ it('leaves a provider without a group alone', async () => {
+ const h = harness();
+ expect(await h.service.resolveSwitchTarget('s1', {
+ agentKind: 'claude-code',
+ model: MODEL,
+ providerId: 'openai',
+ agentDeviceId: null,
+ current: { agentDeviceId: 'elsewhere', providerId: 'openai' },
+ })).toBeNull();
+ expect(h.bindings.size).toBe(0);
+ });
+});
diff --git a/apps/desktop/src/main/provider-group/service.ts b/apps/desktop/src/main/provider-group/service.ts
index a379afe8a1..bf38f08e50 100644
--- a/apps/desktop/src/main/provider-group/service.ts
+++ b/apps/desktop/src/main/provider-group/service.ts
@@ -217,6 +217,22 @@ export interface ProviderGroupService {
}): Promise<ProviderGroupStartContext | null>;
/** 分配后 Agent 没能启动:换下一台,返回新的位置;不该换或没有下一台返回 null(调用方照常报错)。 */
nextAfterStartFailure(context: ProviderGroupStartContext, error: unknown): Promise<ProviderGroupStartContext | null>;
+ /**
+ * 用户在模型选择里把任务换到组那一项、要换电脑(§6,2026-10-11 用户反馈):交接反正要做,按组此刻的状态选一台能用的
+ * 电脑(与新任务同一套组策略),写好绑定,返回那台的位置。`target` 是用户选的位置:不是组那一项(没有组、分享来的供应商)
+ * 或组读不到时返回 null,照用户选的位置换;组里都不能用时抛 PROVIDER_GROUP_UNAVAILABLE_ERROR。选中的就是任务此刻所在
+ * 那台电脑、但供应商不同时也返回 null(同一台电脑上换供应商不走这条路)。
+ */
+ resolveSwitchTarget(
+ sessionId: string,
+ target: {
+ agentKind: AgentKind;
+ model: string;
+ providerId: string | null;
+ agentDeviceId: string | null;
+ current: ProviderGroupRoute;
+ },
+ ): Promise<ProviderGroupRoute | null>;
/**
* 纯判定(同步、无副作用):这次终态错误会不会先由本机制试着换电脑。为 true 时输入协调器先不呈现这次错误
* (红横幅与错误卡):换成了就不出现,没换成再补出来(§6.1)。只看手上现成的:认得出的原因,加上已归组
@@ -1468,6 +1484,43 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
return { ...context, member: pick.member, route, overrideRoute: true };
},
+ async resolveSwitchTarget(sessionId, target) {
+ const { providerId, agentDeviceId } = target;
+ if (!providerId) return null;
+ const source = agentDeviceId === null
+ ? (deps.readGroup(providerId) ? localSource : null)
+ : (deps.remote && !isProviderShareAgentDeviceId(agentDeviceId) ? remoteSource(agentDeviceId) : null);
+ if (!source) return null;
+ const config = await withTimeout(source.readGroup(providerId), PROVIDER_GROUP_ADOPT_TIMEOUT_MS);
+ if (!config) return null;
+ const pick = await source.pick({
+ sessionId,
+ providerId,
+ config,
+ agentKind: target.agentKind,
+ model: target.model,
+ exclude: new Set(),
+ }).catch((error): SourcePick => {
+ deps.log.warn('provider group: asking the group computer failed', { sessionId, error: errorText(error) });
+ return { kind: 'none' };
+ });
+ if (pick.kind === 'none') return null;
+ if (pick.kind === 'unavailable') {
+ deps.log.warn('provider group: no computer available for a task moved into the group', { sessionId, providerId });
+ throw new Error(PROVIDER_GROUP_UNAVAILABLE_ERROR);
+ }
+ const route = source.routeOf(pick.member, source.ownerDeviceId ? null : providerId);
+ if (route.agentDeviceId === target.current.agentDeviceId && route.providerId !== target.current.providerId) return null;
+ await deps.writeBinding(sessionId, bindingFor(source, providerId, pick.member.key));
+ deps.log.info('provider group: chose a computer for a task moved into the group', {
+ sessionId,
+ groupProviderId: providerId,
+ member: pick.member.key,
+ remote: source.ownerDeviceId !== null,
+ });
+ return route;
+ },
+
mayHandleTurnError(sessionId, signals) {
if (!classifyProviderGroupSwitchCause(signals)) return false;
const binding = deps.readBinding(sessionId);
diff --git a/docs/product-rules/provider-groups.md b/docs/product-rules/provider-groups.md
index 5938810447..123318ce20 100644
--- a/docs/product-rules/provider-groups.md
+++ b/docs/product-rules/provider-groups.md
@@ -174,13 +174,17 @@
- 只给从没运行过的任务按策略选电脑。已经运行过、正用着组那一项的任务不按策略挪走:留在原处并纳入组,之后出问题时
照常自动换电脑(2026-10-10 用户要求:老任务也要生效)。包括:
- 建组前就在用这个供应商的任务、回退或清空后原生会话被清掉的任务;
- - 运行过之后才在模型选择里换到组那一项的任务;
+ - 运行过之后才在模型选择里换到组那一项、但 Agent 本来就在组那一项上(组所在电脑的这个供应商)的任务;
- 同账号其他电脑上的老任务:用着另一台电脑上建了组的那个供应商(在组所在电脑本身运行),由任务所在的电脑问组所在电脑
确认它在组里。
- 纳入的时机:启动 Agent 时(问组所在电脑最多等 3 秒,问不到就照常启动);当时没纳入的(一直开着没重新启动、
或启动时问不到)在因 §6.1 的原因失败时先纳入,再照常换电脑,所以已经开着的老任务点重试也会换。
- 不纳入:直接选组里某台电脑或分享来的供应商的任务(用的不是组那一项,§4);因组内电脑被移出、或组被删除而解除过
这个组的任务(§9.4,任务所在的电脑按组记下,之后同一台重新加入、或重建同一个组也不纳入)。
+- **在模型选择里把任务换到组那一项、要换电脑时由组选电脑**(2026-10-11 用户反馈:此前直接落到组所在电脑本身,它撞了周上限,
+ 先报错才换):交接反正要做,发送这条消息、真正换过去的那一刻按组此刻的状态与组策略选一台能用的电脑(与新任务同一套),
+ 直接换到那台并纳入组;组里都不能用时直接提示「供应商组里的电脑现在都不能运行这个任务」,不换、不发,选择保留。选中的就是
+ 任务此刻所在那台电脑、但用的不是组里那个供应商时,照用户选的位置换。组读不到时照用户选的位置换。
- 用户在模型选择里把任务挪到组外的电脑或换成别的供应商,任务即退出组:之后不再自动换电脑,也不把那台的失败
记到组内电脑头上。挪到组里另一台时绑定跟着更新,仍由组管:那台出问题照常自动换电脑,挪过去之后的第一次失败也换
(2026-10-11 用户反馈:此前绑定刚改过的那次失败会被跳过,直接报错);之后再挪回组那一项时按上一条重新纳入。