@@ -270,11 +270,11 @@ export async function buildDynamicTools(input: DynamicToolBuildParams) {
270270 const modelHasVision = params . model . input ?. includes ( "image" ) ?? false ;
271271 const agentDir = params . agentDir ?? resolveAgentDir ( params . config ?? { } , input . sessionAgentId ) ;
272272 const injectedOpenClawCodingToolsFactory = dynamicToolBuildState . openClawCodingToolsFactory ;
273- let agentHarnessModule : typeof import ( "openclaw/plugin-sdk/agent-harness" ) | undefined ;
274- const loadAgentHarnessModule = async ( ) =>
275- ( agentHarnessModule ??= await import ( "openclaw/plugin-sdk/agent-harness" ) ) ;
276- toolBuildStages . mark ( "load-agent-harness-tools" ) ;
277- const nativeExecutionPolicy = resolveCodexNativeExecutionPolicyForDynamicTools ( input ) ;
273+ const nativeExecutionPolicy = resolveCodexNativeExecutionPolicyForRun ( params , {
274+ agentId : input . policyAgentId ,
275+ runtimeSessionKey : input . sandboxSessionKey ,
276+ sandbox : input . sandbox ,
277+ } ) ;
278278 const webSearchPlan = resolveCodexWebSearchPlan ( {
279279 config : params . config ,
280280 disableTools : params . disableTools ,
@@ -455,6 +455,7 @@ export async function buildDynamicTools(input: DynamicToolBuildParams) {
455455 : profileFilteredTools ,
456456 readableAllTools ,
457457 input ,
458+ nativeExecutionPolicy ,
458459 ) ,
459460 readableAllTools ,
460461 input ,
@@ -476,42 +477,42 @@ export async function buildDynamicTools(input: DynamicToolBuildParams) {
476477 ! ( input . pluginConfig . codexDynamicToolsExclude ?? [ ] ) . some (
477478 ( name ) => normalizeCodexDynamicToolName ( name ) === "web_search" ,
478479 ) ;
479- // An injected tool factory can prove that no managed or native search surface exists.
480- // Avoid loading the heavyweight default factory graph when no search policy can affect output .
481- const webSearchPolicy =
482- webSearchPresent || persistentCodexWebSearchSurface
483- ? ( await loadAgentHarnessModule ( ) ) . resolveWebSearchToolPolicy ( {
484- config : params . config ,
485- modelProvider : params . model . provider ,
486- modelId : params . modelId ,
487- agentId : input . policyAgentId ,
488- sessionKey : input . sandboxSessionKey ,
489- sandboxToolPolicy : input . sandbox ?. tools ,
490- messageProvider : resolveCodexMessageToolProvider ( params ) ,
491- agentAccountId : params . agentAccountId ,
492- groupId : params . groupId ,
493- groupChannel : params . groupChannel ,
494- groupSpace : params . groupSpace ,
495- spawnedBy : params . spawnedBy ,
496- senderId : params . senderId ,
497- senderName : params . senderName ,
498- senderUsername : params . senderUsername ,
499- senderE164 : params . senderE164 ,
500- inputProvenance : params . inputProvenance ,
501- trustedInternalHandoff : params . trustedInternalHandoff ,
502- scheduledToolPolicy : params . scheduledToolPolicy ,
503- } )
504- : { allowed : false , persistentAllowed : false } ;
505- const senderScopedWebSearchRestriction =
506- ! webSearchPolicy . allowed && webSearchPolicy . persistentAllowed ;
507- const transientWebSearchRestriction =
508- senderScopedWebSearchRestriction || isCodexMemoryFlushRun ( params ) ;
509- input . onPersistentWebSearchPolicyResolved ?. (
510- webSearchPresent ||
511- ( persistentCodexWebSearchSurface &&
512- transientWebSearchRestriction &&
513- webSearchPolicy . persistentAllowed ) ,
514- ) ;
480+ // An authorized search tool already proves persistent availability. Only absent
481+ // tools need policy resolution to distinguish transient restrictions from denial .
482+ let persistentWebSearchAllowed = webSearchPresent ;
483+ if (
484+ input . onPersistentWebSearchPolicyResolved &&
485+ ! webSearchPresent &&
486+ persistentCodexWebSearchSurface
487+ ) {
488+ const webSearchPolicy = (
489+ await import ( "openclaw/plugin-sdk/agent-harness" )
490+ ) . resolveWebSearchToolPolicy ( {
491+ config : params . config ,
492+ modelProvider : params . model . provider ,
493+ modelId : params . modelId ,
494+ agentId : input . policyAgentId ,
495+ sessionKey : input . sandboxSessionKey ,
496+ sandboxToolPolicy : input . sandbox ?. tools ,
497+ messageProvider : resolveCodexMessageToolProvider ( params ) ,
498+ agentAccountId : params . agentAccountId ,
499+ groupId : params . groupId ,
500+ groupChannel : params . groupChannel ,
501+ groupSpace : params . groupSpace ,
502+ spawnedBy : params . spawnedBy ,
503+ senderId : params . senderId ,
504+ senderName : params . senderName ,
505+ senderUsername : params . senderUsername ,
506+ senderE164 : params . senderE164 ,
507+ inputProvenance : params . inputProvenance ,
508+ trustedInternalHandoff : params . trustedInternalHandoff ,
509+ scheduledToolPolicy : params . scheduledToolPolicy ,
510+ } ) ;
511+ persistentWebSearchAllowed =
512+ webSearchPolicy . persistentAllowed &&
513+ ( ! webSearchPolicy . allowed || isCodexMemoryFlushRun ( params ) ) ;
514+ }
515+ input . onPersistentWebSearchPolicyResolved ?. ( persistentWebSearchAllowed ) ;
515516 const filteredTools = filterCodexDynamicToolsForAllowlist (
516517 visionFilteredTools ,
517518 toolRunContext . runtimeToolAllowlist ,
@@ -657,11 +658,11 @@ export function shouldEnableCodexAppServerNativeToolSurface(
657658 return false ;
658659 }
659660 if (
660- isCodexNativeExecutionBlockedByNodeExecHost ( params , {
661+ ! resolveCodexNativeExecutionPolicyForRun ( params , {
661662 agentId : options . agentId ,
662663 runtimeSessionKey : options . runtimeSessionKey ,
663664 sandbox,
664- } )
665+ } ) . nativeToolSurfaceAllowed
665666 ) {
666667 return false ;
667668 }
@@ -677,24 +678,24 @@ export function shouldEnableCodexAppServerNativeToolSurface(
677678 canCodexAppServerNativeToolSurfaceHonorSandbox ( sandbox , options )
678679 ) ;
679680}
680- /** Returns true when OpenClaw policy requires the Node-owned exec/process tools instead. */
681- function isCodexNativeExecutionBlockedByNodeExecHost (
681+ function resolveCodexNativeExecutionPolicyForRun (
682682 params : EmbeddedRunAttemptParams ,
683683 options : {
684684 agentId ?: string ;
685685 runtimeSessionKey ?: string ;
686686 sandbox ?: OpenClawSandboxContext ;
687687 } = { } ,
688- ) : boolean {
689- return ! resolveCodexNativeExecutionPolicy ( {
688+ ) : CodexNativeExecutionPolicy {
689+ return resolveCodexNativeExecutionPolicy ( {
690690 config : params . config ,
691691 sessionKey : resolveCodexRuntimePolicySessionKey ( params , options . runtimeSessionKey ) ,
692692 sessionId : params . sessionId ,
693693 agentId : options . agentId ,
694694 execOverrides : params . execOverrides ,
695- sandboxAvailable : options . sandbox ?. enabled ,
695+ // A resolved null sandbox is absence; undefined still requests runtime discovery.
696+ sandboxAvailable : options . sandbox === null ? false : options . sandbox ?. enabled ,
696697 readRuntimeSessionEntry : true ,
697- } ) . nativeToolSurfaceAllowed ;
698+ } ) ;
698699}
699700function resolveCodexRuntimePolicySessionKey (
700701 params : EmbeddedRunAttemptParams ,
@@ -822,9 +823,14 @@ function addSandboxShellDynamicToolsIfAvailable(
822823 filteredTools : OpenClawDynamicTool [ ] ,
823824 allTools : OpenClawDynamicTool [ ] ,
824825 input : DynamicToolBuildParams ,
826+ executionPolicy : CodexNativeExecutionPolicy ,
825827) : OpenClawDynamicTool [ ] {
826828 if (
827- ! shouldExposeSandboxExecDynamicTool ( input ) ||
829+ isCodexMemoryFlushRun ( input . params ) ||
830+ ! executionPolicy . nativeToolSurfaceAllowed ||
831+ ! input . sandbox ?. enabled ||
832+ ! input . sandbox . backendId . trim ( ) ||
833+ input . nativeToolSurfaceEnabled !== false ||
828834 isSandboxShellDynamicToolExcluded ( input . pluginConfig )
829835 ) {
830836 return filteredTools ;
@@ -866,22 +872,6 @@ function addSandboxShellDynamicToolsIfAvailable(
866872 } ;
867873 return [ ...filteredTools , sandboxExecTool , sandboxProcessTool ] ;
868874}
869- function shouldExposeSandboxExecDynamicTool ( input : DynamicToolBuildParams ) : boolean {
870- if ( isCodexMemoryFlushRun ( input . params ) ) {
871- return false ;
872- }
873- if (
874- isCodexNativeExecutionBlockedByNodeExecHost ( input . params , {
875- agentId : input . policyAgentId ,
876- runtimeSessionKey : input . sandboxSessionKey ,
877- sandbox : input . sandbox ,
878- } )
879- ) {
880- return false ;
881- }
882- const backendId = input . sandbox ?. enabled ? input . sandbox . backendId . trim ( ) . toLowerCase ( ) : "" ;
883- return Boolean ( backendId && input . nativeToolSurfaceEnabled === false ) ;
884- }
885875function isSandboxShellDynamicToolExcluded ( config : CodexPluginConfig ) : boolean {
886876 return isCodexDynamicToolExcluded ( config , [ "exec" , "sandbox_exec" , "process" , "sandbox_process" ] ) ;
887877}
@@ -927,19 +917,6 @@ function shouldKeepOpenClawShellDynamicTools(
927917 nodePolicy . effectiveExecHost !== "node"
928918 ) ;
929919}
930- function resolveCodexNativeExecutionPolicyForDynamicTools (
931- input : DynamicToolBuildParams ,
932- ) : CodexNativeExecutionPolicy {
933- return resolveCodexNativeExecutionPolicy ( {
934- config : input . params . config ,
935- sessionKey : resolveCodexRuntimePolicySessionKey ( input . params , input . sandboxSessionKey ) ,
936- sessionId : input . params . sessionId ,
937- agentId : input . policyAgentId ,
938- execOverrides : input . params . execOverrides ,
939- sandboxAvailable : input . sandbox ?. enabled ,
940- readRuntimeSessionEntry : true ,
941- } ) ;
942- }
943920/** Applies a normalized tool allowlist while preserving shell aliases for exec/process. */
944921function filterCodexDynamicToolsForAllowlist < T extends { name : string } > (
945922 tools : T [ ] ,
0 commit comments