1- import {
2- embeddedAgentLog ,
3- formatErrorMessage ,
4- isHostScopedAgentToolActive ,
5- } from "openclaw/plugin-sdk/agent-harness-runtime" ;
6- import { buildCodexUserMcpServersThreadConfigPatchForRuntime } from "openclaw/plugin-sdk/codex-mcp-projection" ;
1+ import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime" ;
72import { isIncognitoSessionKey } from "../incognito-session.js" ;
83import { closeCodexStartupClientBestEffort } from "./attempt-client-cleanup.js" ;
9- import {
10- getCodexAppServerClientInstanceId ,
11- resolveCodexAppServerClientInstanceId ,
12- } from "./client.js" ;
13- import { isSystemAgentOnlyCodexDynamicToolAllowlist } from "./dynamic-tool-profile.js" ;
14- import {
15- applyCodexNativeSkillIsolation ,
16- resolveCodexNativeSkillIsolation ,
17- } from "./native-skill-isolation.js" ;
4+ import { resolveCodexAppServerClientInstanceId } from "./client.js" ;
5+ import { applyCodexNativeSkillIsolation } from "./native-skill-isolation.js" ;
186import { buildCodexAppServerConnectionFingerprint } from "./plugin-app-cache-key.js" ;
197import {
208 isCodexPluginThreadBindingStale ,
219 mergeCodexThreadConfigs ,
2210 type CodexPluginThreadConfig ,
2311} from "./plugin-thread-config.js" ;
24- import { isCodexAppServerProfilerEnabled } from "./profiler-flag.js" ;
25- import { flattenCodexDynamicToolFunctions } from "./protocol.js" ;
2612import {
2713 assertCodexBindingMayBeReplaced ,
2814 createCodexSessionGenerationSupersededError ,
29- hashCodexAppServerBindingFingerprint ,
3015 normalizeCodexAppServerBindingModelProvider ,
3116 reclaimCurrentCodexSessionGeneration ,
3217 sessionBindingIdentity ,
@@ -40,23 +25,15 @@ import {
4025 shouldRotateCodexAppServerBindingForRuntime ,
4126 shouldRotateCodexGpt56MultiAgentBinding ,
4227} from "./thread-binding-policy.js" ;
43- import {
44- buildContextEngineBinding ,
45- isContextEngineBindingCompatible ,
46- } from "./thread-context-engine.js" ;
28+ import { isContextEngineBindingCompatible } from "./thread-context-engine.js" ;
4729import {
4830 areDynamicToolFingerprintsCompatible ,
4931 areUserMcpServersFingerprintsCompatible ,
50- codexLegacyDynamicToolsFingerprint as legacyFingerprintDynamicTools ,
51- fingerprintEnvironmentSelection ,
52- fingerprintJsonObject ,
53- fingerprintUserMcpServersConfigPatch ,
54- legacyFingerprintUserMcpServersConfigPatch ,
5532 shouldStartTransientNoToolThread ,
5633} from "./thread-fingerprints.js" ;
5734import { CodexThreadBindingConflictError } from "./thread-lifecycle-errors.js" ;
5835import { resumeExistingCodexThread , startFreshCodexThread } from "./thread-lifecycle-io.js" ;
59- import { createCodexThreadLifecycleTimingTracker } from "./thread-lifecycle-timing .js" ;
36+ import { prepareCodexThreadLifecyclePreflight } from "./thread-lifecycle-preflight .js" ;
6037import type {
6138 CodexAppServerThreadLifecycleBinding ,
6239 CodexStartOrResumeThreadParams ,
@@ -68,14 +45,7 @@ import {
6845 tryReuseCodexLiveThread ,
6946} from "./thread-lifecycle-warm.js" ;
7047import { resolveCodexAppServerThreadModelSelection } from "./thread-model-selection.js" ;
71- import {
72- assertCodexRingZeroHasNoManagedHooks ,
73- buildCodexRingZeroThreadConfigPatch ,
74- CODEX_RING_ZERO_BASE_INSTRUCTIONS ,
75- readCodexInheritedMcpServerNames ,
76- } from "./thread-requests.js" ;
7748import { materializePendingSupervisionBranch } from "./thread-supervision.js" ;
78- import { resolveCodexWebSearchPlan } from "./web-search.js" ;
7949
8050export async function startOrResumeThread (
8151 params : CodexStartOrResumeThreadParams ,
@@ -89,103 +59,26 @@ export async function startOrResumeThread(
8959 config : params . params . config ,
9060 } ) ;
9161 return await params . bindingStore . withLease ( bindingIdentity , async ( ) => {
92- // Thread lifecycle spans are useful when profiling startup churn, but normal
93- // turns should not pay Date.now/span-array overhead while resuming threads.
94- const lifecycleTiming = createCodexThreadLifecycleTimingTracker ( {
95- ...params . timing ,
96- enabled : params . timing ?. enabled ?? isCodexAppServerProfilerEnabled ( params . params . config ) ,
97- } ) ;
98- const legacyDynamicToolsFingerprint = lifecycleTiming . measureSync (
99- "legacy-dynamic-tools-fingerprint" ,
100- ( ) => legacyFingerprintDynamicTools ( params . dynamicTools ) ,
101- ) ;
102- const dynamicToolsFingerprint = lifecycleTiming . measureSync ( "dynamic-tools-fingerprint" , ( ) =>
103- hashCodexAppServerBindingFingerprint ( legacyDynamicToolsFingerprint ) ,
104- ) ;
105- const dynamicToolsContainDeferred = flattenCodexDynamicToolFunctions ( params . dynamicTools ) . some (
106- ( tool ) => tool . deferLoading === true ,
107- ) ;
108- const webSearchPlan = lifecycleTiming . measureSync ( "web-search-plan" , ( ) =>
109- resolveCodexWebSearchPlan ( {
110- config : params . params . config ,
111- disableTools : params . params . disableTools ,
112- nativeToolSurfaceEnabled : params . nativeCodeModeEnabled ,
113- nativeProviderWebSearchSupport : params . nativeProviderWebSearchSupport ,
114- webSearchAllowed : params . webSearchAllowed ,
115- } ) ,
116- ) ;
117- const webSearchThreadConfigFingerprint = fingerprintJsonObject ( webSearchPlan . threadConfig ) ;
118- const networkProxyConfigFingerprint = params . appServer . networkProxy ?. configFingerprint ;
119- const contextEngineBinding = lifecycleTiming . measureSync ( "context-engine-binding" , ( ) =>
120- buildContextEngineBinding ( params . params , params . contextEngineProjection ) ,
121- ) ;
122- const userMcpServersConfigPatch =
123- params . userMcpServersEnabled === false
124- ? undefined
125- : await buildCodexUserMcpServersThreadConfigPatchForRuntime ( params . params . config , {
126- agentId : params . agentId ?? params . params . agentId ,
127- agentDir : params . params . agentDir ,
128- allowLiteralOAuthProjection : params . appServer . connectionClass !== "remote" ,
129- onServerUnavailable : ( serverName , error ) =>
130- embeddedAgentLog . warn ( "skipping unavailable MCP OAuth server" , {
131- serverName,
132- error : formatErrorMessage ( error ) ,
133- } ) ,
134- } ) ;
135- const nativeSkillIsolation = await lifecycleTiming . measure ( "native-skill-isolation" , ( ) =>
136- resolveCodexNativeSkillIsolation ( {
137- client : params . client ,
138- codexHome : params . appServer . start . env ?. CODEX_HOME ,
139- cwd : params . cwd ,
140- home : params . appServer . start . env ?. HOME ,
141- signal : params . signal ,
142- userProfile : params . appServer . start . env ?. USERPROFILE ,
143- } ) ,
144- ) ;
145- const nativeSkillIsolationFingerprint = nativeSkillIsolation
146- ? fingerprintJsonObject ( {
147- version : 1 ,
148- disabledUserSkillPaths : nativeSkillIsolation . disabledUserSkillPaths ,
149- } )
150- : undefined ;
151- const legacyUserMcpServersFingerprint =
152- legacyFingerprintUserMcpServersConfigPatch ( userMcpServersConfigPatch ) ;
153- const userMcpServersFingerprint =
154- fingerprintUserMcpServersConfigPatch ( userMcpServersConfigPatch ) ;
155- const environmentSelectionFingerprint = fingerprintEnvironmentSelection (
156- params . environmentSelection ,
157- ) ;
158- const hostSystemAgentActive =
159- params . hostSystemAgentActive ?? isHostScopedAgentToolActive ( "openclaw" ) ;
160- const ringZeroActive =
161- hostSystemAgentActive && isSystemAgentOnlyCodexDynamicToolAllowlist ( params . params . toolsAllow ) ;
162- if ( ringZeroActive && params . nativeCodeModeEnabled !== false ) {
163- throw new Error ( "Codex ring-zero requires native code mode to be disabled" ) ;
164- }
165- const ringZeroInheritedMcpServerNames = ringZeroActive
166- ? await lifecycleTiming . measure ( "ring-zero-mcp-config-read" , ( ) =>
167- readCodexInheritedMcpServerNames ( params . client , params . cwd , params . signal ) ,
168- )
169- : [ ] ;
170- if ( ringZeroActive ) {
171- await lifecycleTiming . measure ( "ring-zero-config-requirements-read" , ( ) =>
172- assertCodexRingZeroHasNoManagedHooks ( params . client , params . signal ) ,
173- ) ;
174- }
175- const ringZeroConfigFingerprint = ringZeroActive
176- ? fingerprintJsonObject ( {
177- version : 1 ,
178- baseInstructions : CODEX_RING_ZERO_BASE_INSTRUCTIONS ,
179- config : buildCodexRingZeroThreadConfigPatch (
180- params . params ,
181- true ,
182- ringZeroInheritedMcpServerNames ,
183- ) ! ,
184- } )
185- : undefined ;
186- const ringZeroClientInstanceId = ringZeroActive
187- ? getCodexAppServerClientInstanceId ( params . client )
188- : undefined ;
62+ const {
63+ contextEngineBinding,
64+ dynamicToolsContainDeferred,
65+ dynamicToolsFingerprint,
66+ environmentSelectionFingerprint,
67+ hostSystemAgentActive,
68+ legacyDynamicToolsFingerprint,
69+ legacyUserMcpServersFingerprint,
70+ lifecycleTiming,
71+ nativeSkillIsolation,
72+ nativeSkillIsolationFingerprint,
73+ networkProxyConfigFingerprint,
74+ ringZeroActive,
75+ ringZeroClientInstanceId,
76+ ringZeroConfigFingerprint,
77+ ringZeroInheritedMcpServerNames,
78+ userMcpServersConfigPatch,
79+ userMcpServersFingerprint,
80+ webSearchThreadConfigFingerprint,
81+ } = await prepareCodexThreadLifecyclePreflight ( params ) ;
18982 let binding = await lifecycleTiming . measure ( "read-binding" , ( ) =>
19083 params . bindingStore . read ( bindingIdentity ) ,
19184 ) ;
0 commit comments