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: continue orca lead home switch after link close failure

PraiseZhu1d155a8d83cb
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 e6cb0da933..54602b0267 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
@@ -1492,7 +1492,7 @@ describe('远程 Agent:选模型时换 Agent 所在电脑', () => {
expect.stringMatching(/returning|home|close/i),
expect.objectContaining({ sessionId: 's1', code: expect.any(String) }),
);
- expect(applyNow.calls).toEqual(['close', 'db', 'provider', 'boundary', 'pending']);
+ expect(applyNow.calls).toEqual(['close', 'db', 'provider', 'boundary', 'pending', 'bootstrap']);
const pending = relocationHarness({ orcaRole: 'lead', agentDeviceId: 'office-pc' });
await performSessionAgentSwitch(pending.deps, backToTaskComputer);
diff --git a/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts b/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
index 76e111226c..acb9cb0a7a 100644
--- a/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
+++ b/apps/desktop/src/main/maker-ipc/sessionAgentSwitchHandler.ts
@@ -53,6 +53,30 @@ function throwIfAgentSwitchAborted(signal: AbortSignal | undefined): void {
}
}
+/** closeSession 失败时仍可切回本机的链路错误码(离线/不可达/超时/忙)。不认裸 REMOTE_AGENT_UNAVAILABLE / PEER_TOO_OLD。 */
+const AGENT_SWITCH_LINK_CLOSE_CODES = new Set([
+ 'DEVICE_LINK_NOT_CONNECTED',
+ 'DEVICE_LINK_DEVICE_OFFLINE',
+ 'DEVICE_LINK_TIMEOUT',
+ 'DEVICE_LINK_BUSY',
+ 'NOT_CONNECTED',
+ 'DEVICE_OFFLINE',
+ 'TIMEOUT',
+ 'INVOKE_TIMEOUT',
+]);
+
+function agentSwitchLinkCloseErrorCode(error: unknown): string | null {
+ const tokens = new Set<string>();
+ const code = (error as { code?: unknown } | null)?.code;
+ if (typeof code === 'string' && code) tokens.add(code);
+ const message = error instanceof Error ? error.message : String(error ?? '');
+ for (const match of message.matchAll(/\[([A-Z_]+)\]/g)) tokens.add(match[1]!);
+ for (const token of tokens) {
+ if (AGENT_SWITCH_LINK_CLOSE_CODES.has(token)) return token;
+ }
+ return null;
+}
+
/** Phase 2:目标引擎的停泊原生会话绑定(localDb findParkedEngineSession 的投影)。 */
export interface ParkedEngineSessionRef {
sdkSessionId: string;
@@ -851,7 +875,18 @@ export async function performSessionAgentSwitch(
return deps.withCloseSuppressed(sessionId, async () => {
throwIfAgentSwitchAborted(signal);
if (live) {
- await deps.closeSession(sessionId);
+ try {
+ await deps.closeSession(sessionId);
+ } catch (error) {
+ const linkCode = orcaLeadReturnHome ? agentSwitchLinkCloseErrorCode(error) : null;
+ if (!linkCode) throw error;
+ // 旧远程 Session 的 close 已在本地 finish/dispose;链路失败只表示 close 请求没送到对方。
+ // Maker 把该实例标成 error 仍占 activeSessions,bootstrap/lazy-create 会再 close 一次后换新。
+ deps.log.warn('agent-switch: close failed while returning orca lead home; continuing', {
+ sessionId,
+ code: linkCode,
+ });
+ }
}
// 停用轴:提交点重裁决(PR #744 review 第十五轮)—— parked 数据加载、handoff
diff --git a/docs/dev-rules/orca-team-architecture.md b/docs/dev-rules/orca-team-architecture.md
index f533756746..8cbc7b2283 100644
--- a/docs/dev-rules/orca-team-architecture.md
+++ b/docs/dev-rules/orca-team-architecture.md
@@ -395,7 +395,7 @@ Worker turn 被 vendor 报终止型 error,但 interrupted-turn auto-resume 仍
- **新 Worker 也分配**:Worker 的任务记录由启动这一步写入,分配时还读不到,`applyProviderGroupAssignment` 用启动参数作为「从没运行过」的快照(`assignBeforeStart` 的 `startRow`);记录已存在时以记录为准。
- **跟 Lead 就是跟 Lead 的组**:Lead 有组绑定、Worker 选的位置就是 Lead 现在所在且来源没另选(或选的就是 Lead 现在的来源 / 组那一项)、同一种 Agent 时,Worker 改用组那一项(本机的组:任务所在电脑 + 组的供应商;另一台电脑上的组:那台 + 组的供应商),再由组按策略选电脑;组那一项不提供该模型或读不到时照旧跟 Lead 所在那台。`list_available_models` 不指定位置时同口径。实现:`OrcaLeadSessionSnapshot.providerGroupEntry`、`leadProviderGroupFollowCandidate`。
- **只放行组的换位置**:`performSessionAgentSwitch` 对协同任务仍拒绝用户发起的切换,只接受 main 内部带 `providerGroupRelocation` 的 applyNow 调用,且引擎与模型都不变;该标记不从 IPC 读取。用户发起的唯一例外见下一条。
- - **Lead 离线切回本机(2026-10-14 用户裁决的唯一用户发起例外)**:Lead 的 Agent 在另一台电脑、且该电脑模型目录读取失败时,可显式切回本机(`agentDeviceId=null`)。必须同引擎;模型/来源随本机目录改变。Worker、换到其它电脑、换引擎仍拒。入口门控在 `sessionAgentSwitchGate.ts`:仅当 Agent 所在电脑的模型目录读取失败时开放(含瞬时超时;不是对设备在线状态的直接判断,主进程也不复核目录状态)。未验证:未做实机 Orca 协同回归(Lead 回本机后 team/Worker 回报链路)。
+ - **Lead 离线切回本机(2026-10-14 用户裁决的唯一用户发起例外)**:Lead 的 Agent 在另一台电脑、且该电脑模型目录读取失败时,可显式切回本机(`agentDeviceId=null`)。必须同引擎;模型/来源随本机目录改变。Worker、换到其它电脑、换引擎仍拒。入口门控在 `sessionAgentSwitchGate.ts`:仅当 Agent 所在电脑的模型目录读取失败时开放(含瞬时超时;不是对设备在线状态的直接判断,主进程也不复核目录状态)。关闭旧远程会话若因 DEVICE_LINK 离线/超时/忙失败,本机仍提交切回;对方若没收到 close,会在 `REMOTE_AGENT_HOST_IDLE_MS`(3 分钟无 poll)后按 `controller-gone` 回收。未验证:未做实机 Orca 协同回归(Lead 回本机后 team/Worker 回报链路)。
- **换电脑资格与等额度恢复分开**:组的 `isFailoverEligible` 用 `isProviderGroupFailoverEligible`(Worker 可换),额度恢复后自动继续仍用 `isUsageLimitAutoResumeEligible`(Worker 不等,失败交给 Lead)。
- **换过去仍是成员**:`bootstrapSwitchedSession` 对协同任务调 `synthesizeOrcaVendorOptionsFromDb`,新会话带着协同身份、提示词与 `cindy_orca` / `orca_worker_bridge`,不靠下一次发送二次重启。
- **Worker 终态按换电脑那次登记结算**(与 5a 同一原则):输入协调器登记「先不呈现」期间,Worker 的终态不调 `handleWorkerTerminalTurn`,而是按登记 id 暂存(`provider-group/heldWorkerTerminals.ts`;error 事件随 error 行一起暂存,只以 done 收尾的失败在 terminal 末尾暂存)。换成了(续跑已发出)丢掉,auto-bridge pending 保留到续跑那一轮的产品终态再回报;没换成、迟迟没有结局或有人接手时恰好一次回报这次异常终止,账号已切换则不回报。迟到的旧结算只认自己那次登记。