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(codex): 被拒绝的本地配置变更不再打断正在启动的 Codex 任务

Dash261c9ab50f2c
diff --git a/apps/desktop/src/main/hook-control/ipc.ts b/apps/desktop/src/main/hook-control/ipc.ts
index e1b8e44707..fb18e96d7c 100644
--- a/apps/desktop/src/main/hook-control/ipc.ts
+++ b/apps/desktop/src/main/hook-control/ipc.ts
@@ -125,8 +125,12 @@ let codexMcpRefreshPending = false;
let codexMcpRefreshRunning = false;
let codexMcpRefreshRetryTimer: NodeJS.Timeout | null = null;
let latestSlackToolProviderEnabled = false;
+let codexMcpRefreshRetryDelayMs = 0;
-const CODEX_MCP_REFRESH_RETRY_MS = 2_000;
+// busy 重试按指数退避:伙伴等本地 Codex 会话可能连续忙几十分钟,固定 2 秒重试会把日志刷满
+// (2026-10-19 实排:一次启动 987 条 deferred)。新的可用性变化会把退避归零。
+const CODEX_MCP_REFRESH_RETRY_MIN_MS = 2_000;
+const CODEX_MCP_REFRESH_RETRY_MAX_MS = 60_000;
function hookControlAvailable(): boolean {
return getAppCapabilities().canUseCindyAccountServices;
@@ -185,6 +189,7 @@ function disabledHookView(): SlackHookView {
function requestCodexMcpRefreshForSlackAvailability(enabled: boolean): void {
latestSlackToolProviderEnabled = enabled;
codexMcpRefreshPending = true;
+ codexMcpRefreshRetryDelayMs = 0;
if (codexMcpRefreshRetryTimer !== null) {
clearTimeout(codexMcpRefreshRetryTimer);
codexMcpRefreshRetryTimer = null;
@@ -200,13 +205,20 @@ async function drainCodexMcpRefreshForSlackAvailability(): Promise<void> {
codexMcpRefreshPending = false;
try {
await restartCodexAfterAuthModeChange(shutdownCodexEnvironment);
+ codexMcpRefreshRetryDelayMs = 0;
log.info('Codex MCP environment refreshed after Slack provider availability changed', {
enabled: latestSlackToolProviderEnabled,
});
} catch (err) {
codexMcpRefreshPending = true;
- log.warn('Codex MCP refresh deferred after Slack provider availability changed', {
+ codexMcpRefreshRetryDelayMs = codexMcpRefreshRetryDelayMs === 0
+ ? CODEX_MCP_REFRESH_RETRY_MIN_MS
+ : Math.min(codexMcpRefreshRetryDelayMs * 2, CODEX_MCP_REFRESH_RETRY_MAX_MS);
+ // 首次推迟 warn 一次;之后的退避重试只记 debug,避免同一原因刷屏。
+ const logDeferred = codexMcpRefreshRetryDelayMs === CODEX_MCP_REFRESH_RETRY_MIN_MS ? log.warn : log.debug;
+ logDeferred('Codex MCP refresh deferred after Slack provider availability changed', {
enabled: latestSlackToolProviderEnabled,
+ retryInMs: codexMcpRefreshRetryDelayMs,
error: err instanceof Error ? err.message : String(err),
});
break;
@@ -219,7 +231,7 @@ async function drainCodexMcpRefreshForSlackAvailability(): Promise<void> {
codexMcpRefreshRetryTimer = setTimeout(() => {
codexMcpRefreshRetryTimer = null;
void drainCodexMcpRefreshForSlackAvailability();
- }, CODEX_MCP_REFRESH_RETRY_MS);
+ }, codexMcpRefreshRetryDelayMs || CODEX_MCP_REFRESH_RETRY_MIN_MS);
codexMcpRefreshRetryTimer.unref?.();
}
}
diff --git a/packages/maker-core/src/agents/codex/index.test.ts b/packages/maker-core/src/agents/codex/index.test.ts
index 77e4132420..f35c12324d 100644
--- a/packages/maker-core/src/agents/codex/index.test.ts
+++ b/packages/maker-core/src/agents/codex/index.test.ts
@@ -1321,6 +1321,37 @@ describe('CodexAgent spawn configuration', () => {
await agent.dispose();
});
+ it('a refused MCP refresh does not make a starting session reselect its route or respawn', async () => {
+ // 2026-10-19:hook-control 每 2 秒重试一次因伙伴会话 busy 而被拒绝的 MCP 刷新;此前预约就推进
+ // 配置代次,正在启动的会话每次都判定「路由已变」重来,7 次后以 LAZY_CREATE_FAILED 失败。
+ // 被拒绝的变更没有改变任何 host:启动中的会话必须沿用原路由、只 spawn 一次。
+ const started = deferred<void>();
+ const finish = deferred<void>();
+ const resolvePolicy = vi.fn(async () => 'legacy-shared' as const);
+ const agent = new CodexAgent(createDeps({}, {
+ resolveCodexLocalAuthPolicy: resolvePolicy,
+ prepareCodexExtraSpawnConfig: async () => {
+ started.resolve();
+ await finish.promise;
+ return { extraArgs: [], extraEnv: {} };
+ },
+ }));
+ const startup = agent.startSession({ sessionId: 'starting-during-refused-refresh', model: 'gpt-5.4', workingDir: '/repo' });
+ await started.promise;
+ for (let attempt = 0; attempt < 8; attempt++) {
+ const guard = await agent.beginLocalHostCredentialChange('MCP refresh', { allLocalHosts: true });
+ expect(() => guard.assertIdle()).toThrow(/active Codex session/);
+ await expect(guard.retireActiveHost()).rejects.toThrow(/active Codex session/);
+ guard.release();
+ }
+ finish.resolve();
+ const handle = await startup;
+ expect(resolvePolicy).toHaveBeenCalledTimes(1);
+ expect(createdTransports).toHaveLength(1);
+ await handle.close();
+ await agent.dispose();
+ });
+
it('defers MCP refresh during asynchronous control-plane host preparation', async () => {
const started = deferred<void>();
const finish = deferred<void>();
diff --git a/packages/maker-core/src/agents/codex/index.ts b/packages/maker-core/src/agents/codex/index.ts
index 396e40efe0..db35fba1aa 100644
--- a/packages/maker-core/src/agents/codex/index.ts
+++ b/packages/maker-core/src/agents/codex/index.ts
@@ -2375,7 +2375,18 @@ export class CodexAgent extends BaseAgent {
if (pending.length === 0) break;
await Promise.allSettled(pending);
}
- this.bumpHostGeneration(LOCAL_CONFIGURATION_CHANGE_KEY);
+ // 配置代次只在变更真正落地(host 被退役 / finalize 通过 busy 检查)时才推进。
+ // 预约本身已经通过 hostCredentialModeSwitches 挡住新的 host 获取;若在预约时就推进代次,
+ // 一次因 busy 被拒绝、随后 release 的变更也会让所有正在准备 host 的会话判定「路由已变」而
+ // 重来——hook-control 每 2 秒重试一次被拒的 MCP 刷新时,新启动的 Codex 任务会连续撞满 7 次
+ // 重试并以 LAZY_CREATE_FAILED 收场(2026-10-19 实排:伙伴会话长时间 busy,窗口期内任何 Codex
+ // 任务都无法启动)。
+ let generationBumped = false;
+ const commitConfigurationChange = (): void => {
+ if (generationBumped) return;
+ generationBumped = true;
+ this.bumpHostGeneration(LOCAL_CONFIGURATION_CHANGE_KEY);
+ };
let releaseSwitch!: () => void;
const switchPromise = new Promise<void>((resolve) => {
@@ -2413,6 +2424,7 @@ export class CodexAgent extends BaseAgent {
throwOnShutdownFailure: true,
});
}
+ commitConfigurationChange();
};
return {
@@ -2439,6 +2451,7 @@ export class CodexAgent extends BaseAgent {
await retire();
} else {
await this.disposeLocalHostForCredentialChangeUnlocked(key, reason);
+ commitConfigurationChange();
}
}
} finally {
@@ -15399,6 +15412,22 @@ assertRouteCurrent();
});
return;
}
+ // failIfActive 的拒绝必须发生在任何状态改动之前:下面处理 in-flight 创建时会推进 key 代次并
+ // 摘掉 hostPromises,若之后才因 active 抛错,正在创建的 host 会以「superseded」失败,等于一次
+ // 被拒绝的退役仍然打掉了正在启动的会话。in-flight 创建本身也算 active(与 MCP 刷新守卫的
+ // assertIdle 口径一致):正在启动的 host 不能被一次会被拒绝的刷新打断。
+ if (opts.failIfActive) {
+ const host = this.hosts.get(key);
+ const activeUseCount = (host
+ ? this.hostActiveUseCount(key, host)
+ : (this.hostSessionBindingLeases.get(key) ?? 0))
+ + (this.hostPromises.has(key) ? 1 : 0);
+ if (activeUseCount > 0) {
+ throw new Error(
+ `Cannot restart local Codex host while ${activeUseCount} active Codex session(s) are attached`,
+ );
+ }
+ }
const inflight = this.hostPromises.get(key);
if (inflight) {
const bumpedGeneration = this.bumpHostGeneration(key);