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(desktop): daemon rebootstrap 后活跃远端 codex session 统一 detach

yanb6d13e6e5d9a
diff --git a/apps/desktop/src/main/maker-host/__tests__/remote-codex-mcp-recovery.test.ts b/apps/desktop/src/main/maker-host/__tests__/remote-codex-mcp-recovery.test.ts
index 783d9324bf..43869128a1 100644
--- a/apps/desktop/src/main/maker-host/__tests__/remote-codex-mcp-recovery.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/remote-codex-mcp-recovery.test.ts
@@ -25,13 +25,14 @@ function host(id: string): RemoteHost {
function makeDeps(overrides?: Partial<Parameters<typeof refreshRemoteCodexMcpAfterBridgeRecreate>[0]>) {
const warn = vi.fn();
- const checker = (_hostId: string): boolean => false;
+ const checker = vi.fn((_hostId: string): boolean => false);
const deps = {
listRemoteCodexHostIds: () => ['host-a', 'host-b'],
getReadyHost: (id: string) => host(id),
ensureBridgeStarted: async () => ({ port: 38080, serverNames: ['cindy_orca'], bridgeInstanceId: 'bridge-2' }),
getLiveTurnChecker: () => checker,
isCollabEnabled: () => true,
+ detachRemoteCodexSessionsOnHost: vi.fn(),
log: { warn },
...overrides,
};
@@ -72,6 +73,41 @@ describe('refreshRemoteCodexMcpAfterBridgeRecreate', () => {
expect(ensureMock).not.toHaveBeenCalled();
});
+ it('detaches active codex sessions on the host after a successful rebootstrap (R26 P1)', async () => {
+ // ensure 成功且非 live-turn defer ⇒ daemon 已重启 ⇒ 旧 transport 死 —
+ // 必须 detach 让下次 send 走 lazy-resume, 否则 idle-live send 看到
+ // drift 已清会把消息送进死 channel。
+ const { deps, checker } = makeDeps();
+ const detachMock = deps.detachRemoteCodexSessionsOnHost as ReturnType<typeof vi.fn>;
+ refreshRemoteCodexMcpAfterBridgeRecreate(deps);
+ await vi.waitFor(() => {
+ expect(detachMock).toHaveBeenCalledWith('host-a');
+ expect(detachMock).toHaveBeenCalledWith('host-b');
+ });
+ expect(checker).toHaveBeenCalled();
+ });
+
+ it('does not detach when a live turn deferred the rebootstrap (daemon still running the old socket)', async () => {
+ const { deps } = makeDeps({ getLiveTurnChecker: () => () => true });
+ const detachMock = deps.detachRemoteCodexSessionsOnHost as ReturnType<typeof vi.fn>;
+ refreshRemoteCodexMcpAfterBridgeRecreate(deps);
+ await vi.waitFor(() => {
+ expect(ensureMock).toHaveBeenCalledTimes(2);
+ });
+ expect(detachMock).not.toHaveBeenCalled();
+ });
+
+ it('does not detach when the ensure fails (no rebootstrap happened)', async () => {
+ ensureMock.mockResolvedValue({ ok: false, reason: 'forward-failed' });
+ const { deps } = makeDeps({ listRemoteCodexHostIds: () => ['host-a'] });
+ const detachMock = deps.detachRemoteCodexSessionsOnHost as ReturnType<typeof vi.fn>;
+ refreshRemoteCodexMcpAfterBridgeRecreate(deps);
+ await vi.waitFor(() => {
+ expect(ensureMock).toHaveBeenCalledTimes(1);
+ });
+ expect(detachMock).not.toHaveBeenCalled();
+ });
+
it('logs a warning instead of throwing when an ensure reports failure', async () => {
ensureMock.mockResolvedValue({ ok: false, reason: 'forward-failed' });
const { deps, warn } = makeDeps({ listRemoteCodexHostIds: () => ['host-a'] });
diff --git a/apps/desktop/src/main/maker-host/index.ts b/apps/desktop/src/main/maker-host/index.ts
index 794e6ba720..960173c024 100644
--- a/apps/desktop/src/main/maker-host/index.ts
+++ b/apps/desktop/src/main/maker-host/index.ts
@@ -285,6 +285,27 @@ export function setBeforeLocalCodexSessionStartHook(hook: (() => Promise<void>)
_beforeLocalCodexSessionStartHook = hook;
}
+/**
+ * detach 某 host 上活跃的远端 codex session (跳过 turn 中的)。
+ * 使用点:daemon 被 (重) bootstrap 后 (bridge 重建恢复 / shutdown strip) —
+ * 旧 transport 已死, detach 让下次 send 走 lazy-resume 重建
+ * (codex-connector R26 P1)。
+ */
+function detachActiveRemoteCodexSessions(hostId: string, reason: string): void {
+ for (const s of _maker?.listActiveSessions() ?? []) {
+ if (s.agentKind !== 'codex' || s.remoteHostId !== hostId) continue;
+ if (s.isTurnRunning()) continue;
+ void s.detach().catch((err) => {
+ desktopMakerLogger.warn('remote codex session detach after daemon rebootstrap failed', {
+ sessionId: s.id,
+ hostId,
+ reason,
+ message: err instanceof Error ? err.message : String(err),
+ });
+ });
+ }
+}
+
/**
* bridge 重建 / forward 端口重绑后的远端 CC query 失效 (装配版)。
* fresh 标记无条件删 (下次注入重新 forceFresh);无 turn 的直接 detach,
@@ -333,6 +354,10 @@ export function handleCodexEnvironmentShutdownForRemote(): void {
void stripRemoteCodexMcpConfig(host, {
hasLiveTurnOnHost: liveTurnChecker ?? undefined,
});
+ // strip 的 bootstrap (清 env 重启 daemon) 同样杀死旧 transport —
+ // live-turn 豁免内已在 strip 里跳过, 这里 detach 剩余活跃 session
+ // (codex-connector R26 P1 同源)。
+ detachActiveRemoteCodexSessions(hostId, 'bridge-shutdown-strip');
}
}
@@ -388,6 +413,8 @@ export async function ensureCodexMcpBridgeStartedForRemote(): Promise<{
// 恢复路径同闸门 (codex-connector R21 P1):collab 全局禁用时
// ensure 走清理而非重注入。
isCollabEnabled: () => getPluginRegistry().isEnabled('collab'),
+ detachRemoteCodexSessionsOnHost: (hostId) =>
+ detachActiveRemoteCodexSessions(hostId, 'bridge-recreate-rebootstrap'),
log: desktopMakerLogger,
});
// 远端 CC 侧同源恢复 (codex-connector R19 P2):活跃 query 持旧
diff --git a/apps/desktop/src/main/maker-host/remote-codex-mcp-recovery.ts b/apps/desktop/src/main/maker-host/remote-codex-mcp-recovery.ts
index a2600b025c..bffc606f00 100644
--- a/apps/desktop/src/main/maker-host/remote-codex-mcp-recovery.ts
+++ b/apps/desktop/src/main/maker-host/remote-codex-mcp-recovery.ts
@@ -35,6 +35,14 @@ export interface RemoteCodexMcpRecoveryDeps {
* 重新注入回去 (codex-connector R21 P1)。
*/
isCollabEnabled: () => boolean;
+ /**
+ * detach 该 host 上活跃的远端 codex session (跳过 turn 中的)。ensure
+ * 成功 (且非 live-turn defer) ⇒ daemon 已 rebootstrap ⇒ 旧 transport
+ * (到重启前 daemon socket 的长命 channel) 已死 — 不 detach 的话后续
+ * idle-live send 看到 drift 已清会跳过重建, 把消息送进死 transport
+ * (codex-connector R26 P1)。
+ */
+ detachRemoteCodexSessionsOnHost: (hostId: string) => void;
log: { warn: (msg: string, meta?: Record<string, unknown>) => void };
}
@@ -54,6 +62,13 @@ export function refreshRemoteCodexMcpAfterBridgeRecreate(deps: RemoteCodexMcpRec
hostId,
reason: result.reason,
});
+ return;
+ }
+ // ensure 成功且非 live-turn defer ⇒ daemon 已 rebootstrap ⇒ 该 host
+ // 的活跃 session transport 已死 — detach 让下次 send 走 lazy-resume
+ // (codex-connector R26 P1)。live turn 在跑 ⇒ defer 未重启 ⇒ 不动。
+ if (!liveTurnChecker(hostId)) {
+ deps.detachRemoteCodexSessionsOnHost(hostId);
}
});
}