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): keep committed switches complete when binding save fails

Liziddc10f7b8203
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 64841aa057..4ccf8d4fc5 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
@@ -1497,6 +1497,52 @@ describe('换到供应商组那一项:由组选一台能用的电脑(provider-
}));
});
+ it('绑定保存失败不重放已提交的跨电脑切换,仍保留交接并消费意图', async () => {
+ const commitBinding = vi.fn(async () => { throw new Error('binding file lock timeout'); });
+ const h = groupHarness(async () => ({ agentDeviceId: 'office-pc', providerId: 'anthropic-2', commitBinding }));
+ await performSessionAgentSwitch(h.deps, toGroup);
+ await expect(applyPendingAgentSwitchIfIdle(h.deps, 's1', { bootstrapAfterSwitch: true })).resolves.toBeUndefined();
+ expect(h.deps.applyAgentSwitchToDb).toHaveBeenCalledTimes(1);
+ expect(h.deps.insertBoundaryMessage).toHaveBeenCalledWith('s1', expect.objectContaining({
+ fromAgentDeviceId: null, toAgentDeviceId: 'office-pc', handoff: expect.stringContaining('你好'),
+ }));
+ expect(h.deps.setPendingHandoff).toHaveBeenCalledWith('s1', expect.stringContaining('你好'), undefined);
+ expect(h.deps.bootstrapSwitchedSession).toHaveBeenCalledTimes(1);
+ expect(h.pending.get('s1')).toBeUndefined();
+ expect(h.deps.log.warn).toHaveBeenCalledWith(
+ 'agent-switch: provider group binding save failed after switch committed',
+ { sessionId: 's1', err: 'binding file lock timeout' },
+ );
+ await applyPendingAgentSwitchIfIdle(h.deps, 's1');
+ expect(h.deps.applyAgentSwitchToDb).toHaveBeenCalledTimes(1);
+ expect(commitBinding).toHaveBeenCalledTimes(1);
+ });
+
+ it('切换提交失败时不写组绑定,保留选择等待重试', async () => {
+ const commitBinding = vi.fn(async () => undefined);
+ const h = groupHarness(async () => ({ agentDeviceId: 'office-pc', providerId: 'anthropic-2', commitBinding }));
+ vi.mocked(h.deps.applyAgentSwitchToDb).mockRejectedValueOnce(new Error('db write failed'));
+ await performSessionAgentSwitch(h.deps, toGroup);
+ await expect(applyPendingAgentSwitchIfIdle(h.deps, 's1')).rejects.toThrow('db write failed');
+ expect(commitBinding).not.toHaveBeenCalled();
+ expect(h.pending.get('s1')).toBeDefined();
+ expect(h.deps.setPendingHandoff).not.toHaveBeenCalled();
+ });
+
+ it('组选回当前位置:模型已应用后绑定失败不保留或重放选择', async () => {
+ const commitBinding = vi.fn(async () => { throw new Error('disk write failed'); });
+ const h = groupHarness(async () => ({ agentDeviceId: null, providerId: 'xd', commitBinding }));
+ const select = vi.fn(async () => ({ deferred: false }));
+ h.deps.selectSameAgentModel = select;
+ await performSessionAgentSwitch(h.deps, toGroup);
+ await applyPendingAgentSwitchIfIdle(h.deps, 's1');
+ expect(h.pending.get('s1')).toBeUndefined();
+ await applyPendingAgentSwitchIfIdle(h.deps, 's1');
+ expect(select).toHaveBeenCalledTimes(1);
+ expect(commitBinding).toHaveBeenCalledTimes(1);
+ expect(h.deps.applyAgentSwitchToDb).not.toHaveBeenCalled();
+ });
+
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');
diff --git a/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts b/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
index 8d3e4f4a89..a426590290 100644
--- a/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
+++ b/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
@@ -560,6 +560,17 @@ export async function performSessionAgentSwitch(
// (docs/product-rules/provider-groups.md §6,2026-10-11 用户反馈)。登记意图时不选,真正提交时按那一刻的状态选。
let groupResolvedToCurrentPosition = false;
let commitProviderGroupBinding: (() => Promise<void>) | undefined;
+ // 切换已提交后,绑定文件失败不能把整次切换当成未发生,否则重试会丢掉原电脑的交接。
+ async function commitBindingAfterSwitch(): Promise<void> {
+ try {
+ await commitProviderGroupBinding?.();
+ } catch (error) {
+ deps.log.warn('agent-switch: provider group binding save failed after switch committed', {
+ sessionId,
+ err: error instanceof Error ? error.message : String(error),
+ });
+ }
+ }
if (
params.applyNow === true &&
params.providerGroupRelocation !== true &&
@@ -626,7 +637,7 @@ export async function performSessionAgentSwitch(
// 组选回当前位置并且模型应用真正成功(非 deferred/superseded)时才持久化新绑定;
// 没落地的调用不应该殊到已纳入组(chatgpt-codex-connector P2, PR #5792 review)。
if (groupResolvedToCurrentPosition && !result.deferred && !result.superseded) {
- await commitProviderGroupBinding?.();
+ await commitBindingAfterSwitch();
}
return {
switched: false,
@@ -780,7 +791,7 @@ export async function performSessionAgentSwitch(
// closeSession / assertModelRouteUsable 任何一步抛错都不会让新绑定抔先持久化
// (chatgpt-codex-connector P2, PR #5792 review)。
if (commitProviderGroupBinding) {
- await commitProviderGroupBinding();
+ await commitBindingAfterSwitch();
}
const boundaryContent: AgentSwitchBoundaryContent = {