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): preserve failover lease across session close (#5880) (#5894)

Lizi0bdda6e5317c
diff --git a/apps/desktop/src/main/provider-group/__tests__/failoverSessionClose.test.ts b/apps/desktop/src/main/provider-group/__tests__/failoverSessionClose.test.ts
new file mode 100644
index 0000000000..b2db3e8c18
--- /dev/null
+++ b/apps/desktop/src/main/provider-group/__tests__/failoverSessionClose.test.ts
@@ -0,0 +1,489 @@
+/**
+ * #5880:运行中远端组员离线,终态错误 drain 宽限(250ms)到点关闭旧会话,与 failover 取得重试句柄赛跑。
+ * 用真实的 AgentInputCoordinator、真实的会话关闭接线(createSessionBindingLifecycle → runSessionCloseCleanup →
+ * onSessionClosed,按 register 的 captureCloseContext 计算 preserveAutoResumeIntent)与真实的供应商组 service;
+ * 只把组内电脑、交接与任务记录换成内存实现。
+ */
+import type { SessionStatus } from '@cindy/maker-core';
+import type { ProviderView } from '@cindy/model-providers';
+import { describe, expect, it, vi } from 'vitest';
+
+import { CONTINUE_AFTER_ERROR_PROMPT } from '../../../shared/interruptedTurn';
+import type { AgentInputQueuedMessage } from '../../../shared/agentInputQueue';
+import type { ProviderGroupConfig, ProviderGroupMember } from '../../../shared/providerGroup';
+import { AgentInputCoordinator, type AgentInputCoordinatorDeps } from '../../maker-ipc/agent-input-coordinator';
+import { shouldPreserveWaitingContinuationOnlyAutoResume } from '../../maker-ipc/interruptedTurnAutoResume';
+import { createSessionBindingLifecycle, runSessionCloseCleanup } from '../../maker-ipc/sessionBindingLifecycle';
+import type { ProviderGroupBinding } from '../bindings';
+import type { ProviderGroupDirectory, ResolvedMemberState, ResolvedProviderGroupMember } from '../directory';
+import { createProviderGroupRouter } from '../router';
+import {
+ createProviderGroupService,
+ type ProviderGroupServiceDeps,
+ type ProviderGroupSessionRow,
+} from '../service';
+
+vi.mock('../../localDb/ipc/messages.js', () => ({ createMessage: vi.fn(async () => ({})) }));
+vi.mock('../../localDb/ipc/sessions.js', () => ({ touchUserSendInDb: vi.fn(async () => {}) }));
+vi.mock('../../logger.js', () => ({
+ createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
+}));
+
+const SID = 'task-5880';
+const MODEL = 'claude-opus-5-5';
+const LOCAL: ProviderGroupMember = {
+ key: 'local', kind: 'local', agentDeviceId: null, providerId: 'anthropic', limit: 4, weight: 1, paused: false,
+};
+const MAC_B: ProviderGroupMember = {
+ key: 'device:mac-b:anthropic-b', kind: 'device', agentDeviceId: 'mac-b', providerId: 'anthropic-b', limit: 4, weight: 1, paused: false,
+};
+/** 现场的决定:phase=run action=switch cause=unavailable connection=lost code=REMOTE_AGENT_UNAVAILABLE。 */
+const B_LOST = {
+ reason: 'remote_agent_lost',
+ message: '[REMOTE_AGENT_UNAVAILABLE] The computer running this agent is unreachable.',
+};
+
+function view(id: string): ProviderView {
+ return {
+ id,
+ name: id,
+ agents: ['claude-code'],
+ connected: true,
+ models: { 'claude-code': [{ id: MODEL, name: MODEL, efforts: [], defaultEffort: null }] },
+ routing: { 'claude-code': {} },
+ } as unknown as ProviderView;
+}
+
+function item(clientId: string, text: string): AgentInputQueuedMessage {
+ const createOpts = {
+ agentKind: 'claude-code',
+ workingDir: '/repo',
+ model: MODEL,
+ effort: 'medium',
+ permissionMode: 'default',
+ userPrompt: '',
+ makerMemoryEnabled: true,
+ displayReasoning: 'summarized',
+ } as AgentInputQueuedMessage['createOpts'];
+ return {
+ clientId,
+ text,
+ persistedContent: text,
+ model: MODEL,
+ effort: 'medium',
+ permissionMode: 'default',
+ workingDir: '/repo',
+ chatMessage: { clientId, role: 'user', content: text, isStreaming: false, createdAt: '2026-10-12T08:26:00.000Z' },
+ createOpts,
+ } as AgentInputQueuedMessage;
+}
+
+async function flush() {
+ for (let i = 0; i < 200; i++) await Promise.resolve();
+}
+
+function gate() {
+ let open!: () => void;
+ const opened = new Promise<void>((resolve) => { open = resolve; });
+ return { opened, open };
+}
+
+interface FakeSession {
+ id: string;
+ closeReason: 'unexpected' | 'agent-switch';
+ listeners: Set<(status: SessionStatus) => void>;
+ onStatusChange(listener: (status: SessionStatus) => void): () => void;
+ setInteractionListener(listener: unknown): void;
+}
+
+function setup(options: { bComesBack?: boolean } = {}) {
+ let running = false;
+ const config: ProviderGroupConfig = { strategy: 'order', autoSwitch: true, members: [MAC_B, LOCAL] };
+ const offline = new Set<string>([MAC_B.key]);
+ const bindings = new Map<string, ProviderGroupBinding>([[SID, { providerId: 'anthropic', memberKey: MAC_B.key, at: 1 }]]);
+ const row: ProviderGroupSessionRow = {
+ agentKind: 'claude-code', model: MODEL, providerId: MAC_B.providerId, agentDeviceId: MAC_B.agentDeviceId,
+ remoteHostId: null, sdkSessionId: 'native-on-b',
+ };
+
+ // ── 真实的输入协调器 ──
+ const sendToAgent = vi.fn<AgentInputCoordinatorDeps['sendToAgent']>(async (_sid, _message, _createOpts, sendOpts) => {
+ const persist = sendOpts.persistUserMessage;
+ (persist as { onPersisting?: () => void } | undefined)?.onPersisting?.();
+ await persist?.onPersisted?.();
+ running = true;
+ return { kind: 'session-dispatch', source: 'test', dispatched: true };
+ });
+ let service!: ReturnType<typeof createProviderGroupService>;
+ const coordinator = new AgentInputCoordinator({
+ sendToAgent,
+ steerToAgent: vi.fn(async () => {}),
+ abortSession: vi.fn(async () => {}),
+ isTurnRunning: () => running,
+ hasPendingInteraction: () => false,
+ getAgentKind: () => 'claude-code',
+ getSdkSessionId: async () => 'native-on-b',
+ hasAssistantProgressAfter: async () => true,
+ emitProjection: () => {},
+ // 现场:这次错误没有被中断自愈接管(否则不会走到供应商组)。
+ isResumableTurnErrorCandidate: () => false,
+ onResumableTurnError: () => null,
+ providerGroupSwitchCandidate: (sessionId, signals) =>
+ service.mayHandleTurnError(sessionId, signals) ? 'unavailable' : null,
+ onUsageLimitedTurnError: (sessionId, signals, _item, token) => {
+ turnError = service.onTurnError(sessionId, signals, token, hooks);
+ },
+ });
+ let turnError: Promise<void> | null = null;
+ const hooks = {
+ beforeFallback: vi.fn(),
+ resumed: vi.fn(),
+ reconnecting: vi.fn(),
+ switching: vi.fn(),
+ continuing: vi.fn(),
+ };
+
+ // ── 真实的会话关闭接线(与 register.ts 的 captureCloseContext / cleanupClosedSession 相同的判定与顺序) ──
+ const resetAutoResume = vi.fn();
+ const closeContexts: Array<{ closeReason: string; preserveAutoResumeIntent: boolean }> = [];
+ const lifecycle = createSessionBindingLifecycle<FakeSession, { closeReason: string; preserveAutoResumeIntent: boolean }>({
+ log: { warn: vi.fn() },
+ restoreInteractionListener: () => {},
+ beforeReplace: () => {},
+ onBind: () => {},
+ broadcastStatus: () => {},
+ captureCloseContext: (session) => {
+ const coordinatorAttemptToken = coordinator.getAutoResumeAttemptToken(session.id);
+ const preserveAutoResumeIntent = shouldPreserveWaitingContinuationOnlyAutoResume({
+ closeReason: session.closeReason,
+ // 这次错误没有中断自愈的尝试:register 的 pendingContinuationOnlyAutoResumeRebuilds / guard / book 都没有它。
+ leasedAttemptToken: undefined,
+ guardIsCurrentAttempt: false,
+ bookIsCurrentAttempt: false,
+ coordinatorAttemptToken,
+ hasLiveSchedule: false,
+ hasQueuedAutoResume: coordinator.hasQueuedAutoResume(session.id),
+ isContinuationOnly: coordinator.isContinuationOnlyAutoResume(session.id),
+ });
+ const context = { closeReason: session.closeReason, preserveAutoResumeIntent };
+ closeContexts.push(context);
+ return context;
+ },
+ cleanupClosedSession: (session, context) => {
+ runSessionCloseCleanup(context.preserveAutoResumeIntent, {
+ cleanupInteractions: () => {},
+ preserveAutoResume: () => {},
+ resetAutoResume,
+ shouldPreserveInputBoundary: () => false,
+ closeInputCoordinator: (closeOptions) => coordinator.onSessionClosed(session.id, closeOptions),
+ cleanupRuntimeState: () => {},
+ });
+ },
+ beforeCloseTeardown: () => {},
+ finalizeClosedSession: () => {},
+ });
+ function openSession(): FakeSession {
+ const session: FakeSession = {
+ id: SID,
+ closeReason: 'unexpected',
+ listeners: new Set(),
+ onStatusChange(listener) {
+ this.listeners.add(listener);
+ return () => this.listeners.delete(listener);
+ },
+ setInteractionListener: () => {},
+ };
+ const registration = lifecycle.beginBinding(session);
+ if (registration) lifecycle.attachStatusListener(registration);
+ return session;
+ }
+ /** Session.close():drain 宽限到点自己关(closeReason 没登记 = unexpected),或交接时按 agent-switch 关。 */
+ function closeSession(session: FakeSession, reason: FakeSession['closeReason']) {
+ session.closeReason = reason;
+ for (const listener of [...session.listeners]) listener('closed');
+ }
+ let live = openSession();
+
+ // ── 真实的供应商组 service,组内电脑与交接用内存实现 ──
+ const probe = vi.fn(async (agentDeviceId: string): Promise<ResolvedMemberState> =>
+ agentDeviceId === MAC_B.agentDeviceId && offline.has(MAC_B.key) ? 'offline' : 'ok');
+ const directory: ProviderGroupDirectory = {
+ async resolveMembers(_providerId, current) {
+ return current.members.map((m): ResolvedProviderGroupMember => offline.has(m.key)
+ ? { member: m, label: m.key, state: 'offline' }
+ : { member: m, label: `${m.key}-name`, state: 'ok', view: view(m.providerId) });
+ },
+ async listCandidates() { return []; },
+ async readDeviceCatalog() { return []; },
+ probe,
+ memberLabel: (m) => m.label ?? m.key,
+ invalidate: vi.fn(),
+ };
+ const router = createProviderGroupRouter({
+ directory,
+ readGroup: (id) => (id === 'anthropic' ? config : null),
+ listBindings: () => Object.fromEntries(bindings),
+ isTurnRunning: () => false,
+ now: () => 1_000_000,
+ random: () => 0,
+ });
+ let readRowGate: { opened: Promise<void>; open: () => void } | null = null;
+ let probeGate: { opened: Promise<void>; open: () => void } | null = null;
+ let held: (() => void) | null = null;
+ const leaseResults: Array<object | null> = [];
+ const deps = {
+ router,
+ directory,
+ readGroup: (id: string) => (id === 'anthropic' ? config : null),
+ readBinding: (id: string) => bindings.get(id) ?? null,
+ writeBinding: vi.fn(async (id: string, binding: { providerId: string; memberKey: string } | null) => {
+ if (binding) bindings.set(id, { ...binding, at: 2 });
+ else bindings.delete(id);
+ }),
+ isReleased: () => false,
+ markReleased: vi.fn(async () => {}),
+ // 「让 B 的终态错误进入处理后暂停一次 readSessionRow」。
+ readSessionRow: vi.fn(async () => {
+ const pending = readRowGate;
+ readRowGate = null;
+ if (pending) await pending.opened;
+ return { ...row };
+ }),
+ resolveImplicitProvider: vi.fn(async () => 'anthropic'),
+ persistRoute: vi.fn(async () => {}),
+ hasAssistantHistory: vi.fn(async () => true),
+ isFailoverEligible: vi.fn(async () => true),
+ // 与 register.ts 相同的接线。
+ leaseRecovery: (sessionId: string, token: number) => {
+ const lease = coordinator.leaseUsageLimitRecovery(sessionId, token);
+ leaseResults.push(lease);
+ return lease;
+ },
+ isLeaseCurrent: (sessionId: string, lease: object) => coordinator.isUsageLimitRecoveryLeaseCurrent(sessionId, lease),
+ rearmContinue: (sessionId: string, lease: object, resumeAt: number | null) =>
+ coordinator.rearmUsageLimitWait(sessionId, lease, resumeAt),
+ cancelContinue: (sessionId: string, token: number) => {
+ coordinator.cancelUsageLimitWait(sessionId, token);
+ },
+ // 交接:关闭旧会话(agent-switch)、改写任务记录、在新电脑上打开新会话。
+ switchAgentLocation: vi.fn(async (_id: string, route: { agentDeviceId: string | null; providerId: string | null }, switchOptions?: { isCurrent?: () => boolean }) => {
+ if (switchOptions?.isCurrent && !switchOptions.isCurrent()) throw new Error('superseded');
+ closeSession(live, 'agent-switch');
+ row.agentDeviceId = route.agentDeviceId;
+ row.providerId = route.providerId;
+ row.sdkSessionId = null;
+ live = openSession();
+ }),
+ isTurnRunning: vi.fn(() => false),
+ hasLiveSession: vi.fn(() => true),
+ continueSession: vi.fn(async (sessionId: string, token: number, info: Parameters<ProviderGroupServiceDeps['continueSession']>[2]) =>
+ coordinator.continueAfterUsageLimitReset(sessionId, token, info)),
+ fallback: vi.fn(),
+ readResetAt: vi.fn(() => null),
+ now: () => 1_000_000,
+ sleep: vi.fn(async () => {
+ // 等原电脑恢复的第一次退避之后,B 回来了(短暂断线)。
+ if (options.bComesBack) offline.delete(MAC_B.key);
+ }),
+ log: { info: vi.fn(), warn: vi.fn() },
+ } satisfies ProviderGroupServiceDeps;
+ const originalProbe = probe.getMockImplementation()!;
+ probe.mockImplementation(async (...args) => {
+ const pending = probeGate;
+ probeGate = null;
+ if (pending) await pending.opened;
+ return originalProbe(...args);
+ });
+ service = createProviderGroupService(deps);
+
+ /** 在 B 上开始一轮,B 离线:终态错误进入协调器(随后交给供应商组)。 */
+ async function failOnB(failure: { message: string; signals: Record<string, unknown> } = {
+ message: B_LOST.message,
+ signals: { reason: B_LOST.reason },
+ }) {
+ coordinator.enqueue(SID, item('q-1', 'long running task'));
+ await flush();
+ expect(sendToAgent).toHaveBeenCalledTimes(1);
+ running = false;
+ coordinator.onTurnEvent(SID, 'error', failure.message, failure.signals);
+ }
+
+ const markTried = vi.spyOn(router, 'markTried');
+ return {
+ coordinator,
+ service,
+ router,
+ markTried,
+ deps,
+ hooks,
+ row,
+ bindings,
+ sendToAgent,
+ leaseResults,
+ closeContexts,
+ resetAutoResume,
+ pauseNextReadSessionRow: () => {
+ readRowGate = gate();
+ held = readRowGate.open;
+ },
+ pauseNextProbe: () => {
+ probeGate = gate();
+ held = probeGate.open;
+ },
+ /** 放行暂停的那一步,等这次错误处理走完。 */
+ async releaseAndSettle() {
+ held?.();
+ held = null;
+ await flush();
+ await turnError;
+ await flush();
+ },
+ failOnB,
+ /** 终态错误 drain 宽限到点:Session.close() 自己关掉(没有登记关闭原因)。 */
+ drainTimeoutClose: () => closeSession(live, 'unexpected'),
+ };
+}
+
+function expectSwitchedToLocalAndContinued(h: ReturnType<typeof setup>) {
+ expect(h.deps.switchAgentLocation).toHaveBeenCalledTimes(1);
+ expect(h.deps.switchAgentLocation.mock.calls[0]?.[1]).toMatchObject({ agentDeviceId: null, providerId: 'anthropic' });
+ expect(h.bindings.get(SID)?.memberKey).toBe(LOCAL.key);
+ expect(h.row.agentDeviceId).toBeNull();
+ expect(h.deps.continueSession).toHaveBeenCalledTimes(1);
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).toEqual({ type: 'user', content: CONTINUE_AFTER_ERROR_PROMPT });
+ expect(h.hooks.resumed).toHaveBeenCalledTimes(1);
+ expect(h.deps.fallback).not.toHaveBeenCalled();
+}
+
+describe('provider group failover vs. the terminal-error drain close (#5880)', () => {
+ it('the drain-timeout close does not preserve the auto-resume intent for this error', async () => {
+ const h = setup();
+ h.pauseNextReadSessionRow();
+ await h.failOnB();
+ await flush();
+ h.drainTimeoutClose();
+ expect(h.closeContexts).toEqual([{ closeReason: 'unexpected', preserveAutoResumeIntent: false }]);
+ expect(h.resetAutoResume).toHaveBeenCalledTimes(1);
+ });
+
+ it('closes before the lease is taken: still switches to this computer and continues the turn', async () => {
+ const h = setup();
+ h.pauseNextReadSessionRow();
+ await h.failOnB();
+ await flush();
+ // failover 停在 readSessionRow:此时 drain 宽限到点、会话关闭清理先跑。
+ expect(h.deps.readSessionRow).toHaveBeenCalledTimes(1);
+ h.drainTimeoutClose();
+ // 关闭清理只撤了限额等待候选:这次错误仍是当前状态(仍按换电脑先不呈现),用户没有接手。
+ expect(h.coordinator.getProviderGroupSwitchHoldId(SID)).not.toBeNull();
+ await h.releaseAndSettle();
+ expect(h.leaseResults[0]).not.toBeNull();
+ expectSwitchedToLocalAndContinued(h);
+ });
+
+ it('control: lease taken before the close (close lands while waiting for B) also switches and continues', async () => {
+ const h = setup();
+ h.pauseNextProbe();
+ await h.failOnB();
+ await flush();
+ expect(h.leaseResults).toEqual([expect.any(Object)]);
+ h.drainTimeoutClose();
+ await h.releaseAndSettle();
+ expectSwitchedToLocalAndContinued(h);
+ });
+
+ it('a short disconnect still continues on B when the close lands before the lease', async () => {
+ const h = setup({ bComesBack: true });
+ h.pauseNextReadSessionRow();
+ await h.failOnB();
+ await flush();
+ h.drainTimeoutClose();
+ await h.releaseAndSettle();
+ expect(h.deps.switchAgentLocation).not.toHaveBeenCalled();
+ expect(h.bindings.get(SID)?.memberKey).toBe(MAC_B.key);
+ expect(h.deps.continueSession).toHaveBeenCalledTimes(1);
+ expect(h.deps.continueSession.mock.calls[0]?.[2]).toMatchObject({ agentReconnect: { computer: MAC_B.key } });
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ });
+
+ it('does not continue after the user stops during the window, even though the session was closed first', async () => {
+ const h = setup();
+ h.pauseNextReadSessionRow();
+ await h.failOnB();
+ await flush();
+ h.drainTimeoutClose();
+ // 用户按停止(协调器的 Stop 收掉错误与重试入口)。
+ h.coordinator.stop(SID);
+ await h.releaseAndSettle();
+ expect(h.deps.switchAgentLocation).not.toHaveBeenCalled();
+ expect(h.deps.continueSession).not.toHaveBeenCalled();
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not continue the old error once the user sends a new message during the window', async () => {
+ const h = setup();
+ h.pauseNextReadSessionRow();
+ await h.failOnB();
+ await flush();
+ h.drainTimeoutClose();
+ h.coordinator.enqueue(SID, item('q-2', 'something else'));
+ h.service.noteUserAction(SID);
+ await flush();
+ await h.releaseAndSettle();
+ expect(h.deps.switchAgentLocation).not.toHaveBeenCalled();
+ expect(h.deps.continueSession).not.toHaveBeenCalled();
+ // 只有用户自己那条新消息被发出,没有替旧错误续跑。
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).not.toEqual({ type: 'user', content: CONTINUE_AFTER_ERROR_PROMPT });
+ });
+
+ // 用户亲自接手发生在取得句柄之后、换电脑开始之前(读组设置、资格检查、读任务记录的 await 期间):
+ // 后续每一步都停下,不冷却、不记为试过、不换电脑(provider-groups.md §6.1)。
+ const B_LIMITED = { message: "You've hit your session limit", signals: { sdkError: 'rate_limit' } };
+ function expectNothingDoneFor(h: ReturnType<typeof setup>) {
+ expect(h.router.coolingUntil('anthropic', MAC_B.key)).toBeNull();
+ expect(h.markTried).not.toHaveBeenCalled();
+ expect(h.deps.directory.invalidate).not.toHaveBeenCalled();
+ expect(h.deps.switchAgentLocation).not.toHaveBeenCalled();
+ expect(h.deps.continueSession).not.toHaveBeenCalled();
+ expect(h.deps.fallback).not.toHaveBeenCalled();
+ }
+
+ it('control: a usage limit on B cools B, marks it tried and switches to this computer', async () => {
+ const h = setup();
+ await h.failOnB(B_LIMITED);
+ await h.releaseAndSettle();
+ expect(h.router.coolingUntil('anthropic', MAC_B.key)).not.toBeNull();
+ expect(h.markTried).toHaveBeenCalledWith(SID, MAC_B.key);
+ expectSwitchedToLocalAndContinued(h);
+ });
+
+ it('a new message sent while the checks are pending stops before cooling or marking B tried', async () => {
+ const h = setup();
+ h.pauseNextReadSessionRow();
+ await h.failOnB(B_LIMITED);
+ await flush();
+ // 与 register 发送入口相同:先作废换电脑这一趟,再进队。
+ h.service.noteUserAction(SID);
+ h.coordinator.enqueue(SID, item('q-2', 'something else'));
+ await flush();
+ await h.releaseAndSettle();
+ expectNothingDoneFor(h);
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).not.toEqual({ type: 'user', content: CONTINUE_AFTER_ERROR_PROMPT });
+ });
+
+ it('changing the model while the checks are pending stops the switch even though the error is still shown', async () => {
+ const h = setup();
+ h.pauseNextReadSessionRow();
+ await h.failOnB(B_LIMITED);
+ await flush();
+ // 与 register 换模型入口相同:作废换电脑这一趟、撤销额度等待;错误与重试入口仍在(句柄本身还有效)。
+ h.service.noteUserAction(SID);
+ h.coordinator.cancelUsageLimitWait(SID);
+ await h.releaseAndSettle();
+ expectNothingDoneFor(h);
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+ });
+});
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 0d184506c7..5dd176c8a1 100644
--- a/apps/desktop/src/main/provider-group/__tests__/remoteGroup.test.ts
+++ b/apps/desktop/src/main/provider-group/__tests__/remoteGroup.test.ts
@@ -375,6 +375,20 @@ describe('switching computers within a group on another computer', () => {
expect(gone.deps.switchAgentLocation).not.toHaveBeenCalled();
});
+ it('keeps the retry entry when the session closes while the task record is read (#5880)', async () => {
+ const h = bound({ config: undefined });
+ h.probe.mockResolvedValueOnce('offline').mockResolvedValueOnce('ok');
+ // 读任务记录期间终态错误的 drain 宽限到点、会话关闭:关闭撤销了限额等待候选,之后再取句柄会落空。
+ h.deps.readSessionRow.mockImplementation(async () => {
+ h.deps.leaseRecovery.mockReturnValue(null);
+ return h.row;
+ });
+ await h.service.onTurnError('s1', { message: '[REMOTE_AGENT_DEVICE_UNREACHABLE] gone' }, 3);
+ expect(h.deps.continueSession).toHaveBeenCalledWith('s1', 7, expect.objectContaining({
+ agentReconnect: { computer: '' },
+ }));
+ });
+
it('waits before sending for its own computer when the group computer cannot be read and the session must be reopened', async () => {
const reopen = bound({ config: undefined });
Object.assign(reopen.deps, { hasLiveSession: () => false });
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 eddb164229..dda1b08990 100644
--- a/apps/desktop/src/main/provider-group/__tests__/service.test.ts
+++ b/apps/desktop/src/main/provider-group/__tests__/service.test.ts
@@ -493,7 +493,9 @@ describe('onTurnError (automatic switch)', () => {
h.service.onTurnError('s1', { sdkError: 'rate_limit' }, 1);
await flush();
expect(h.deps.fallback).toHaveBeenCalledTimes(1);
- expect(h.deps.leaseRecovery).not.toHaveBeenCalled();
+ // 句柄在第一次 await 之前就取得(只读,#5880);不归组的任务不会凭它重新挂上等待或换电脑。
+ expect(h.deps.rearmContinue).not.toHaveBeenCalled();
+ expect(h.deps.switchAgentLocation).not.toHaveBeenCalled();
expect(h.bindings.size).toBe(0);
}
});
@@ -638,8 +640,8 @@ describe('onTurnError (automatic switch)', () => {
const h = harness();
h.bindings.set('s1', { providerId: 'anthropic', memberKey: 'local', at: 1 });
h.deps.switchAgentLocation.mockRejectedValueOnce(new Error('[REMOTE_AGENT_DEVICE_UNREACHABLE] unreachable'));
- // 第一次交接失败后,那次错误已被新 turn / 中断自愈接管。
- h.deps.isLeaseCurrent.mockReturnValueOnce(true).mockReturnValueOnce(true).mockReturnValue(false);
+ // 第一次交接失败后,那次错误已被新 turn / 中断自愈接管(开始换电脑前先复核一次,之后选电脑前后各一次)。
+ h.deps.isLeaseCurrent.mockReturnValueOnce(true).mockReturnValueOnce(true).mockReturnValueOnce(true).mockReturnValue(false);
h.service.onTurnError('s1', { sdkError: 'rate_limit' }, 1);
await flush();
expect(h.deps.switchAgentLocation).toHaveBeenCalledTimes(1);
@@ -1277,6 +1279,38 @@ describe('onTurnError for a shared user (the sharer’s group picks the computer
expect(h.bindings.size).toBe(0);
});
+ it('keeps the retry entry when the session closes while the task record is read (#5880)', async () => {
+ const h = guestHarness();
+ h.guestSwitch.offer('s1', 'token-aaaaaaaaaaaaaaaa');
+ // 读任务记录期间会话关闭:关闭撤销了限额等待候选,之后再取句柄会落空。
+ h.deps.readSessionRow.mockImplementation(async () => {
+ h.deps.leaseRecovery.mockReturnValue(null);
+ return h.row;
+ });
+ h.service.onTurnError('s1', { sdkError: 'rate_limit' }, 7);
+ await flush();
+ expect(h.deps.switchAgentLocation).toHaveBeenCalledTimes(1);
+ expect(h.deps.continueSession).toHaveBeenCalledTimes(1);
+ expect(h.deps.fallback).not.toHaveBeenCalled();
+ });
+
+ it('drops the claimed token when the error stopped being current while the task record was read', async () => {
+ const h = guestHarness();
+ h.guestSwitch.offer('s1', 'token-aaaaaaaaaaaaaaaa');
+ // 读任务记录期间那次错误已被新的一轮接管(句柄已不是当前状态)。
+ h.deps.readSessionRow.mockImplementation(async () => {
+ h.deps.isLeaseCurrent.mockReturnValue(false);
+ return h.row;
+ });
+ h.service.onTurnError('s1', { sdkError: 'rate_limit' }, 7);
+ await flush();
+ expect(h.deps.switchAgentLocation).not.toHaveBeenCalled();
+ expect(h.deps.continueSession).not.toHaveBeenCalled();
+ expect(h.deps.fallback).not.toHaveBeenCalled();
+ // 下一次打开不带凭证:组所在电脑不会替它换电脑。
+ expect(h.guestSwitch.takeForOpen('s1')).toBeUndefined();
+ });
+
it('leaves the error to the existing handling without a token from the group computer', async () => {
const h = guestHarness();
h.service.onTurnError('s1', { message: '[REMOTE_AGENT_SHARE_PAUSED] paused' }, 3);
diff --git a/apps/desktop/src/main/provider-group/service.ts b/apps/desktop/src/main/provider-group/service.ts
index 86b0275dad..76339f97c8 100644
--- a/apps/desktop/src/main/provider-group/service.ts
+++ b/apps/desktop/src/main/provider-group/service.ts
@@ -962,18 +962,19 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
/**
* 登记一次进行中的换电脑:用户在途中接手(重试、发消息、换模型)或那次错误已不是当前状态时,之后的每一步都停手。
* `handBack` 在不再换电脑时交回原有的报错与额度重置后自动继续;之前的交接若已关掉旧会话,换一个对当前状态
- * 有效的令牌。
+ * 有效的令牌。`run` 在取得句柄时就已登记(failover),取得之后、开始之前的检查期间用户接手同样停手:一步都不做。
*/
async function withSwitchRun(
sessionId: string,
signals: InterruptedTurnErrorSignals,
token: number,
lease: object,
+ run: { superseded: boolean },
hooks: ProviderGroupTurnErrorHooks | undefined,
body: (isCurrent: () => boolean, handBack: () => void) => Promise<void>,
): Promise<void> {
- const { run, done } = trackRun(sessionId);
const isCurrent = () => !run.superseded && deps.isLeaseCurrent(sessionId, lease);
+ if (!isCurrent()) return;
const handBack = () => {
if (!isCurrent()) return;
hooks?.beforeFallback?.();
@@ -985,8 +986,6 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
} catch (error) {
deps.log.warn('provider group: automatic switch failed', { sessionId, error: errorText(error) });
handBack();
- } finally {
- done();
}
}
@@ -999,12 +998,35 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
): Promise<boolean> {
const decision = decide(sessionId, signals, 'run');
if (decision.action !== 'switch') return false;
+ // 交接会关闭旧会话(关闭撤销限额等待):先取得这次错误的重试入口。终态错误后会话也可能在下面的检查期间自己关闭
+ // (drain 宽限到点关闭重建),所以在第一次 await 之前取得;同时登记这一趟,下面检查期间用户接手(发消息、重试、
+ // 换模型)或新一轮都让它停手(run.superseded / isLeaseCurrent)。
+ const lease = deps.leaseRecovery(sessionId, token);
+ const { run, done } = trackRun(sessionId);
+ try {
+ return await failoverRun(sessionId, signals, token, hooks, decision, lease, run);
+ } finally {
+ done();
+ }
+ }
+
+ async function failoverRun(
+ sessionId: string,
+ signals: InterruptedTurnErrorSignals,
+ token: number,
+ hooks: ProviderGroupTurnErrorHooks | undefined,
+ decision: ProviderGroupFailureDecision & { action: 'switch' },
+ lease: object | null,
+ run: { superseded: boolean },
+ ): Promise<boolean> {
const { cause } = decision;
if (!deps.readBinding(sessionId) && !(await adoptOnFailure(sessionId))) {
- return guestFailover(sessionId, signals, token, cause, hooks);
+ return guestFailover(sessionId, signals, token, lease, run, cause, hooks);
}
const bound = await loadBound(sessionId);
- if (!bound) return decision.connection ? reconnectWithoutGroup(sessionId, signals, token, decision, hooks) : false;
+ if (!bound) {
+ return decision.connection ? reconnectWithoutGroup(sessionId, signals, token, lease, run, decision, hooks) : false;
+ }
const { binding, source, config } = bound;
if (!config) {
await releaseDeletedGroup(sessionId, groupOf(source, binding.providerId));
@@ -1016,11 +1038,10 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
if (!row?.model || row.remoteHostId) return false;
const current = await verifyBinding(sessionId, source, binding, config, row);
if (!current) return false;
- // 交接会关闭旧会话(关闭撤销限额等待):先取得这次错误的重试入口。用户已接手时什么都不做。
- const lease = deps.leaseRecovery(sessionId, token);
+ // 用户已接手时什么都不做。
if (!lease) return true;
const boundRow = { ...row, model: row.model };
- await withSwitchRun(sessionId, signals, token, lease, hooks, async (isCurrent, handBack) => {
+ await withSwitchRun(sessionId, signals, token, lease, run, hooks, async (isCurrent, handBack) => {
// 连不上先等那台恢复:恢复了留在原电脑继续这一轮(不交接),等不到再换。
if (decision.connection) {
let waited = false;
@@ -1080,6 +1101,8 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
sessionId: string,
signals: InterruptedTurnErrorSignals,
token: number,
+ lease: object | null,
+ run: { superseded: boolean },
decision: ProviderGroupFailureDecision & { action: 'switch' },
hooks: ProviderGroupTurnErrorHooks | undefined,
): Promise<boolean> {
@@ -1089,9 +1112,8 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
const row = await deps.readSessionRow(sessionId);
if (!row?.model || row.remoteHostId || !row.agentDeviceId || !row.providerId) return false;
const route = { agentDeviceId: row.agentDeviceId, providerId: row.providerId };
- const lease = deps.leaseRecovery(sessionId, token);
if (!lease) return true;
- await withSwitchRun(sessionId, signals, token, lease, hooks, async (isCurrent, handBack) => {
+ await withSwitchRun(sessionId, signals, token, lease, run, hooks, async (isCurrent, handBack) => {
const outcome = await awaitReturn(sessionId, route, binding.memberKey, isCurrent, (attempt) => {
hooks?.reconnecting?.(attempt, PROVIDER_GROUP_RECONNECT_MAX_ATTEMPTS);
}, {
@@ -1165,6 +1187,8 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
sessionId: string,
signals: InterruptedTurnErrorSignals,
token: number,
+ lease: object | null,
+ run: { superseded: boolean },
cause: ProviderGroupFailureCause,
hooks: ProviderGroupTurnErrorHooks | undefined,
): Promise<boolean> {
@@ -1175,13 +1199,13 @@ export function createProviderGroupService(deps: ProviderGroupServiceDeps): Prov
if (!row?.model || row.remoteHostId || !shareRoute || !isProviderShareAgentDeviceId(shareRoute)) return false;
if (!(await deps.isFailoverEligible(sessionId))) return false;
if (!guestSwitch.claim(sessionId)) return false;
- const lease = deps.leaseRecovery(sessionId, token);
- if (!lease) {
+ // 检查期间用户已接手或错误已被新的一轮接管:不换,作废刚登记给下一次打开的凭证。
+ if (!lease || run.superseded || !deps.isLeaseCurrent(sessionId, lease)) {
guestSwitch.release(sessionId);
return true;
}
const model = row.model;
- await withSwitchRun(sessionId, signals, token, lease, hooks, async (isCurrent, handBack) => {
+ await withSwitchRun(sessionId, signals, token, lease, run, hooks, async (isCurrent, handBack) => {
try {
// 位置仍是同一个分享:强制重新交接、新建会话,打开时带上凭证,由组所在电脑换一台。
await deps.switchAgentLocation(sessionId, {