Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(codex): fork 走一次性 host 并瘦身响应,避免熔断全部活跃会话 (#2171)
Dashcffc95a7679f
diff --git a/apps/desktop/src/main/__tests__/fork.test.ts b/apps/desktop/src/main/__tests__/fork.test.tsindex b0b7d9c4ab..1944774333 100644--- a/apps/desktop/src/main/__tests__/fork.test.ts+++ b/apps/desktop/src/main/__tests__/fork.test.ts@@ -313,6 +313,8 @@ describe('forkSessionAtMessage', () => {expect(forkSdkSessionMock).toHaveBeenCalledWith('codex', {sourceSdkSessionId: 'codex-thread-source',+ model: 'gpt-5.4',+ providerId: 'xd',upToMessageId: undefined,tailTurnsToDrop: 2,title: '[Fork] Project A',@@ -404,6 +406,8 @@ describe('forkSessionAtMessage', () => {expect(forkSdkSessionMock).toHaveBeenCalledWith('codex', {sourceSdkSessionId: 'codex-thread-source',+ model: 'gpt-5.5',+ providerId: 'xd',upToMessageId: undefined,title: '[Fork·已剥离] Project A',workingDir: '/work',@@ -748,6 +752,8 @@ describe('forkSessionAtMessage', () => {expect(forkSdkSessionMock).toHaveBeenCalledWith('codex', {sourceSdkSessionId: 'parked-codex-thread',+ model: 'gpt-5.4',+ providerId: null,upToMessageId: undefined,tailTurnsToDrop: 2,title: '[Fork] Project A',@@ -1091,6 +1097,8 @@ describe('forkSessionAtMessage', () => {expect(forkSdkSessionMock).toHaveBeenCalledWith('codex', {sourceSdkSessionId: 'codex-thread-source',+ model: 'claude-sonnet-4-6',+ providerId: 'xd',upToMessageId: undefined,tailTurnsToDrop: 1,title: '[Fork] Project A',diff --git a/apps/desktop/src/main/maker-orchestration/fork.ts b/apps/desktop/src/main/maker-orchestration/fork.tsindex 4a0a41bdb7..30a7f7cf21 100644--- a/apps/desktop/src/main/maker-orchestration/fork.ts+++ b/apps/desktop/src/main/maker-orchestration/fork.ts@@ -568,6 +568,7 @@ export async function forkSessionAtMessage(const agentKind = dbToMakerAgentKind(forkSource.agentKind);const forkResult = await getMaker().forkSdkSession(agentKind, {sourceSdkSessionId: forkSource.sdkSessionId,+ ...(isCodex ? { model: forkSource.model, providerId: forkSource.providerId } : {}),upToMessageId: assistantUuid,...(tailTurnsToDrop !== undefined ? { tailTurnsToDrop } : {}),title: newTitle,@@ -704,6 +705,8 @@ export async function forkSessionStripEncrypted(sourceSessionId: string): Promis: `[Fork·已剥离] ${source.title}`;const { newSdkSessionId, uuidMap } = await getMaker().forkSdkSession('codex', {sourceSdkSessionId: source.sdkSessionId,+ model: source.model,+ providerId: source.providerId,upToMessageId: undefined,title: newTitle,workingDir: source.workingDir ?? undefined,diff --git a/packages/maker-core/src/agents/codex/app-server/protocol.ts b/packages/maker-core/src/agents/codex/app-server/protocol.tsindex 1d49481793..0aafb479ad 100644--- a/packages/maker-core/src/agents/codex/app-server/protocol.ts+++ b/packages/maker-core/src/agents/codex/app-server/protocol.ts@@ -468,6 +468,12 @@ export interface ThreadForkParams {sandbox?: SandboxMode;/** Per-request config overrides, including named permission profile definitions. */config?: Record<string, unknown>;+ /**+ * 只返回 thread 元数据与 live fork state,不把完整历史塞进单条 NDJSON response。+ * 与 thread/resume.excludeTurns 同族;超长历史 thread 的 fork 响应体与历史成+ * 正比、无上界,曾实测单行 31MiB 超过 client maxLineBytes(16MiB)熔断整条连接。+ */+ excludeTurns?: boolean;[k: string]: unknown;}diff --git a/packages/maker-core/src/agents/codex/index.test.ts b/packages/maker-core/src/agents/codex/index.test.tsindex e937646ec7..a262cd0abb 100644--- a/packages/maker-core/src/agents/codex/index.test.ts+++ b/packages/maker-core/src/agents/codex/index.test.ts@@ -433,11 +433,12 @@ function installFakeHost(discardPendingDescendantLineage: vi.fn(),};+ const getHost = vi.fn(async () => host);Object.defineProperty(agent, 'getHost', {- value: async () => host,+ value: getHost,});- return host;+ return Object.assign(host, { getHost });}async function nextEvent(iterator: AsyncIterator<AgentEvent>): Promise<AgentEvent> {@@ -5006,8 +5007,12 @@ describe('CodexAgent MCP thread context hooks', () => {});expect(result.newSdkSessionId).toMatch(/^fork-thread-/);- expect(createdTransports).toHaveLength(1);+ // Fork runs on its own ephemeral host (fault-radius isolation): a second+ // transport is spawned for the fork and retired when it finishes, while+ // the explicit credential session host stays untouched and open.+ expect(createdTransports).toHaveLength(2);expect(createdTransports[0].closed).toBe(false);+ expect(createdTransports[1].closed).toBe(true);expect(prepareCodexLocalCredentialModeSwitch).not.toHaveBeenCalled();await handle.close();@@ -13430,7 +13435,7 @@ describe('CodexAgent.forkSdkSession', () => {expect(host.unsubscribeThread).not.toHaveBeenCalledWith('source-thread-id');});- it('retires the captured host and rejects when a forked child cannot be unloaded', async () => {+ it('retires the ephemeral fork host and rejects when a forked child cannot be unloaded', async () => {const agent = new CodexAgent(createDeps());const host = installFakeHost(agent);host.unsubscribeThread.mockRejectedValueOnce(new Error('unsubscribe failed'));@@ -13448,17 +13453,102 @@ describe('CodexAgent.forkSdkSession', () => {})).rejects.toThrow('unsubscribe failed');expect(retireHostKey).toHaveBeenCalledWith(- 'local',- 'Codex fork child fork-thread-id could not be unloaded safely',+ expect.stringMatching(/^local-fork:/),+ 'Codex fork host is single-use',expect.objectContaining({failIfActive: false,- logPrefix: 'codex fork child cleanup',- expectedGeneration: 0,+ logPrefix: 'codex fork host cleanup',}),);expect(host.unsubscribeThread).not.toHaveBeenCalledWith('source-thread-id');});+ it('retires the ephemeral fork host after a successful fork', async () => {+ const agent = new CodexAgent(createDeps());+ installFakeHost(agent);+ const retireHostKey = vi+ .spyOn(+ agent as unknown as { retireHostKey: (...args: unknown[]) => Promise<void> },+ 'retireHostKey',+ )+ .mockResolvedValue(undefined);++ await agent.forkSdkSession({+ sourceSdkSessionId: 'source-thread-id',+ upToMessageId: undefined,+ tailTurnsToDrop: 0,+ });++ expect(retireHostKey).toHaveBeenCalledTimes(1);+ expect(retireHostKey).toHaveBeenCalledWith(+ expect.stringMatching(/^local-fork:/),+ 'Codex fork host is single-use',+ expect.objectContaining({+ failIfActive: false,+ logPrefix: 'codex fork host cleanup',+ }),+ );+ });++ it('requests excludeTurns on thread/fork when the daemon supports it', async () => {+ const agent = new CodexAgent(createDeps());+ const host = installFakeHost(agent, undefined, { userAgent: 'mock-codex/0.145.0' });++ await agent.forkSdkSession({+ sourceSdkSessionId: 'source-thread-id',+ upToMessageId: undefined,+ tailTurnsToDrop: 0,+ });++ expect(host.request).toHaveBeenCalledWith(Method.ThreadFork, {+ threadId: 'source-thread-id',+ persistExtendedHistory: true,+ excludeTurns: true,+ });+ });++ it('omits excludeTurns for daemons older than 0.145.0', async () => {+ const agent = new CodexAgent(createDeps());+ const host = installFakeHost(agent, undefined, { userAgent: 'mock-codex/0.144.6' });++ await agent.forkSdkSession({+ sourceSdkSessionId: 'source-thread-id',+ upToMessageId: undefined,+ tailTurnsToDrop: 0,+ });++ const forkParams = host.request.mock.calls.find(([m]) => m === Method.ThreadFork)?.[1] as+ | { excludeTurns?: boolean }+ | undefined;+ expect(forkParams).toBeDefined();+ expect(forkParams?.excludeTurns).toBeUndefined();+ });++ it('preserves source provider credentials on an isolated fork host', async () => {+ const agent = new CodexAgent(createDeps());+ const host = installFakeHost(agent);++ await agent.forkSdkSession({+ sourceSdkSessionId: 'source-thread-id',+ model: 'gpt-5.4',+ providerId: 'custom-provider',+ upToMessageId: undefined,+ tailTurnsToDrop: 0,+ });++ expect(host.getHost).toHaveBeenCalledWith(+ undefined,+ 'provider-oauth',+ expect.objectContaining({+ keyOverride: expect.stringMatching(/^local-fork:/),+ hostPurpose: 'control-plane',+ }),+ );+ expect(host.request).toHaveBeenCalledWith(Method.ThreadFork, expect.objectContaining({+ threadId: 'source-thread-id',+ }));+ });+it('forks from a temporary rollout copy without unsafe payload lines', async () => {const agent = new CodexAgent(createDeps());let copied = '';diff --git a/packages/maker-core/src/agents/codex/index.ts b/packages/maker-core/src/agents/codex/index.tsindex 179ebcb915..7bece0ed5a 100644--- a/packages/maker-core/src/agents/codex/index.ts+++ b/packages/maker-core/src/agents/codex/index.ts@@ -22,6 +22,7 @@import os from 'node:os';import path from 'node:path';+import { randomUUID } from 'node:crypto';import { promises as fs } from 'node:fs';import { structuredPatch } from 'diff';@@ -373,6 +374,22 @@ function isLocalControlPlaneHostKey(key: string): boolean {return key.startsWith(LOCAL_CONTROL_PLANE_HOST_PREFIX);}+/**+ * 一次性 fork host key。thread/fork 的响应体与源 thread 历史成正比、无上界,+ * 不能与正在服务活跃 session 的共享 host 同进程 —— 单条超限 NDJSON 会熔断整条+ * 连接,把无关 session 一起拖下水(故障半径隔离,见 forkSdkSession)。+ * 随机后缀保证并发 fork 互不复用。+ */+const LOCAL_FORK_HOST_PREFIX = 'local-fork:';++function localForkHostKey(): string {+ return `${LOCAL_FORK_HOST_PREFIX}${randomUUID()}`;+}++function isLocalForkHostKey(key: string): boolean {+ return key.startsWith(LOCAL_FORK_HOST_PREFIX);+}+/*** maker permissionMode → app-server { approvalPolicy, approvalsReviewer, sandbox }。*@@ -466,6 +483,18 @@ function supportsCodexResumeExcludeTurns(userAgent: string | undefined): booleanreturn codexUserAgentAtLeast(userAgent, [0, 125, 0]);}+/**+ * `thread/fork.excludeTurns` was verified against the app-server bundled with+ * Codex 0.145.0. Without it, the fork response inlines the entire source-thread+ * history in one NDJSON line — a 47MB rollout produced a 31MiB single line that+ * exceeded the client's 16MiB maxLineBytes guard and killed the whole+ * connection (2026-08-08 field incident). Older daemons keep the legacy+ * full-history fork response.+ */+function supportsCodexForkExcludeTurns(userAgent: string | undefined): boolean {+ return codexUserAgentAtLeast(userAgent, [0, 145, 0]);+}+const READONLY_REFERENCES_PERMISSION_PROFILE = 'cindy-readonly-references';/**@@ -10048,41 +10077,30 @@ export class CodexAgent extends BaseAgent {const log = this.deps.logger.child('codex/fork');const tailTurnsToDrop = normalizeTailTurnsToDrop(opts.tailTurnsToDrop);let stripCopyPath: string | undefined;- let utilityHostKey: string | undefined;- let utilityHost: AppServerHost | undefined;- let utilityHostGeneration: number | undefined;- let utilityHostWasRegistered = false;+ // 故障半径隔离(2026-08-08 实排):thread/fork 的响应体与源 thread 历史成正比、+ // 无上界 —— 47MB rollout 实测产出 31MiB 单行 NDJSON,超过 client 16MiB+ // maxLineBytes 守卫后整条连接被熔断,当时共享 utility host 上挂着的 5 个活跃+ // session 全部同时报错。fork 是离线控制面操作(不跑 turn、无订阅者),改用+ // 唯一 key 的一次性 app-server:超限只让 fork 自己失败,不波及活跃任务。+ const forkHostKey = localForkHostKey();+ let forkHost: AppServerHost | undefined;const createdThreadIds = new Set<string>();const cleanupCreatedThreads = async (): Promise<void> => {- if (!utilityHost || !utilityHostKey || createdThreadIds.size === 0) return;+ if (!forkHost || createdThreadIds.size === 0) return;for (const threadId of createdThreadIds) {try {- // Codex 0.145 keeps a forked child loaded in the shared app-server.- // Unload it before the new Cindy Session resumes with its own MCP- // instance URL; otherwise thread/resume.config is ignored and the- // child remains bound to the utility host's spawn-level URL.- await utilityHost.unsubscribeThread(threadId);+ // Codex 0.145 keeps a forked child loaded in the app-server. Unload+ // it before the new Cindy Session resumes with its own MCP instance+ // URL; otherwise thread/resume.config is ignored and the child+ // remains bound to this host's spawn-level URL. The ephemeral host+ // is retired right after, but graceful unload also flushes the+ // child's state before the process dies.+ await forkHost.unsubscribeThread(threadId);} catch (error) {- const reason = `Codex fork child ${threadId} could not be unloaded safely`;- log.warn('fork child cleanup failed; retiring shared app-server', {+ log.warn('fork child cleanup failed; ephemeral fork host will be retired', {threadId,error: error instanceof Error ? error.message : String(error),});- try {- await this.retireHostKey(utilityHostKey, reason, {- failIfActive: false,- logPrefix: 'codex fork child cleanup',- ...(utilityHostWasRegistered ? { expectedHost: utilityHost } : {}),- ...(utilityHostGeneration !== undefined- ? { expectedGeneration: utilityHostGeneration }- : {}),- });- } catch (retireError) {- log.warn('fork child cleanup host retire threw', {- threadId,- error: retireError instanceof Error ? retireError.message : String(retireError),- });- }throw error;}}@@ -10093,20 +10111,28 @@ export class CodexAgent extends BaseAgent {upToMessageId: opts.upToMessageId,tailTurnsToDrop,stripEncryptedReasoning: opts.stripEncryptedReasoning === true,- note: 'Codex 精确 fork: thread/fork 后按需 thread/rollback 新 thread 尾部 turn',+ forkHostKey,+ note: 'Codex 精确 fork: 独立一次性 host 上 thread/fork 后按需 thread/rollback 新 thread 尾部 turn',});try {- const utility = await this.getUtilityHost();- utilityHostKey = utility.key;- utilityHost = utility.host;- utilityHostGeneration = this.hostGenerations.get(utility.key) ?? 0;- utilityHostWasRegistered = this.hosts.get(utility.key) === utility.host;- const { host } = utility;- await host.ensureStarted();+ const forkCredentialMode = resolveAgentCredentialMode({+ agentKind: 'codex',+ providerId: opts.providerId,+ model: opts.model,+ });+ const host = await this.getHost(undefined, forkCredentialMode, {+ keyOverride: forkHostKey,+ hostPurpose: 'control-plane',+ });+ forkHost = host;+ const initResp = await host.ensureStarted();+ // createSafeForkRolloutCopy 扫描 this.codexHome;之前由共享 host 启动时填充,+ // 隔离后 fork 可能是本进程第一台 host,须自己补上。+ if (initResp.codexHome) this.codexHome = initResp.codexHome;// Imported Codex threads may still live under another CODEX_HOME. Resume// already asks the desktop host to link/adopt their state and rollout;// fork must cross the same preparation boundary before thread/fork or the- // utility app-server cannot resolve a freshly imported thread.+ // fork app-server cannot resolve a freshly imported thread.const preparedRolloutResult = await this.deps.prepareCodexResumeSession?.(opts.sourceSdkSessionId);const preparedRolloutPath = typeof preparedRolloutResult === 'string'? preparedRolloutResult@@ -10121,6 +10147,10 @@ export class CodexAgent extends BaseAgent {const params: ThreadForkParams = {threadId: opts.sourceSdkSessionId,persistExtendedHistory: true,+ // 响应体瘦身:fork 后 Cindy 自己的会话数据负责历史展示,thread.turns 全量+ // 回传只会撑爆单行上限。老 daemon 不认识该字段则保持 legacy 行为 —— 此时+ // 一次性 host 的隔离仍兜住故障半径。+ ...(supportsCodexForkExcludeTurns(initResp.userAgent) ? { excludeTurns: true } : {}),...(stripCopyPath ? { path: stripCopyPath } : {}),...(opts.workingDir ? { cwd: opts.workingDir } : {}),};@@ -10132,6 +10162,8 @@ export class CodexAgent extends BaseAgent {threadId: newSdkSessionId,numTurns: tailTurnsToDrop,};+ // thread/rollback 没有 excludeTurns 对应物,响应仍可能携带完整历史;+ // 超限时熔断的只是这台一次性 host,活跃 session 不受影响。const rollbackResp = await host.request<ThreadRollbackResponse>(Method.ThreadRollback,rollbackParams,@@ -10146,6 +10178,20 @@ export class CodexAgent extends BaseAgent {try {await cleanupCreatedThreads();} finally {+ // 一次性 host 用完即收,无论成败。key 唯一、无 session 绑定,+ // retire 不会波及任何共享 host 或活跃会话。+ if (forkHost) {+ await this.retireHostKey(forkHostKey, 'Codex fork host is single-use', {+ failIfActive: false,+ logPrefix: 'codex fork host cleanup',+ expectedHost: forkHost,+ }).catch((err) => {+ log.warn('fork host retire failed', {+ forkHostKey,+ err: err instanceof Error ? err.message : String(err),+ });+ });+ }if (stripCopyPath) {await fs.rm(path.dirname(stripCopyPath), { recursive: true, force: true }).catch((err) => {log.warn('strip encrypted rollout temp cleanup failed', {@@ -10238,10 +10284,10 @@ export class CodexAgent extends BaseAgent {async forceDisposeLocalHostForAuthChange(reason = 'CodexAgent local auth changed'): Promise<void> {const keys = new Set<string>([hostKey()]);for (const key of this.hosts.keys()) {- if (isLocalControlPlaneHostKey(key)) keys.add(key);+ if (isLocalControlPlaneHostKey(key) || isLocalForkHostKey(key)) keys.add(key);}for (const key of this.hostPromises.keys()) {- if (isLocalControlPlaneHostKey(key)) keys.add(key);+ if (isLocalControlPlaneHostKey(key) || isLocalForkHostKey(key)) keys.add(key);}await Promise.all(Array.from(keys, (key) =>this.retireHostKey(key, reason, {@@ -10402,7 +10448,10 @@ export class CodexAgent extends BaseAgent {// 立即 push, 让所有 live thread 通过 server 端 reload_user_config 拿到新值try {const localSessionHosts = Array.from(this.hosts.entries()).filter(- ([key]) => !key.startsWith('remote:') && !isLocalControlPlaneHostKey(key),+ ([key]) =>+ !key.startsWith('remote:') &&+ !isLocalControlPlaneHostKey(key) &&+ !isLocalForkHostKey(key),);if (localSessionHosts.length === 0) {log.info('setMemory ◀ no live app-server, will apply on next session');diff --git a/packages/maker-core/src/types/events.ts b/packages/maker-core/src/types/events.tsindex ad7ef02281..a57af0f8eb 100644--- a/packages/maker-core/src/types/events.ts+++ b/packages/maker-core/src/types/events.ts@@ -362,6 +362,13 @@ export interface RewindCommitResult {export interface ForkSdkSessionOptions {/** 源 session 的 SDK sessionId (从 sessions.sdk_session_id 取)。 */sourceSdkSessionId: string;+ /**+ * 源原生 session 的模型与供应商来源。Codex 的隔离 fork host 需要用相同上下文+ * 解析 credential mode;否则 provider-oauth 源任务在本机没有 fallback 凭证时会+ * 被误判为未登录。Claude/PI 不消费这两个字段。+ */+ model?: string;+ providerId?: string | null;/*** 截断锚点 — 必须是 SDK assistant 消息的 uuid (调用方反查 + 跳 subagent)。* Claude 必填; Codex 协议无 message uuid, 此字段被 CodexAgent 忽略,