@@ -11,7 +11,12 @@ import {
1111 wrapToolWithBeforeToolCallHook ,
1212} from "openclaw/plugin-sdk/agent-harness-runtime" ;
1313import { readMemoryArtifactProvenance } from "openclaw/plugin-sdk/memory-core-host-runtime-core" ;
14- import { createAgentHarnessHostCapabilitiesForTest } from "openclaw/plugin-sdk/plugin-test-runtime" ;
14+ import {
15+ createAgentHarnessHostCapabilitiesForTest ,
16+ createMockPluginRegistry ,
17+ initializeGlobalHookRunner ,
18+ resetGlobalHookRunner ,
19+ } from "openclaw/plugin-sdk/plugin-test-runtime" ;
1520import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
1621import { dynamicToolBuildState } from "./dynamic-tool-build-state.js" ;
1722import {
@@ -193,6 +198,7 @@ async function buildDynamicToolsForTest(
193198 nativeToolSurfaceEnabled : true ,
194199 runAbortController : new AbortController ( ) ,
195200 sessionAgentId : "main" ,
201+ policyAgentId : params . sandboxAgentId ?? options . sessionAgentId ?? "main" ,
196202 pluginConfig : { } ,
197203 onYieldDetected : ( ) => undefined ,
198204 ...options ,
@@ -383,62 +389,112 @@ describe("Codex app-server dynamic tool build", () => {
383389 ] ) ;
384390 } ) ;
385391
386- it ( "filters disabled-native replacements with the canonical conversation profile" , async ( ) => {
387- setOpenClawCodingToolsFactoryForTests ( ( options ) =>
388- createOpenClawCodingTools ( options ) . filter ( ( tool ) =>
389- [ "read" , "write" , "edit" , "apply_patch" , "exec" , "process" ] . includes ( tool . name ) ,
390- ) ,
391- ) ;
392- const workspaceDir = path . join ( tempDir , "workspace" ) ;
393- const params = createParams ( path . join ( tempDir , "policy-session.jsonl" ) , workspaceDir ) ;
394- params . disableTools = false ;
395- params . runtimePlan = createCodexRuntimePlanFixture ( ) ;
396- params . conversationToolPolicy = { deny : [ "exec" , "process" , "write" , "edit" ] } ;
397- const tools = await buildDynamicToolsForTest ( params , workspaceDir , {
398- nativeToolSurfaceEnabled : false ,
399- } ) ;
400- const names = tools . map ( ( tool ) => tool . name ) ;
401-
402- expect ( names . toSorted ( ) ) . toEqual ( [ "apply_patch" , "read" ] ) ;
403- const conversationNativeTools = await buildDynamicToolsForTest ( params , workspaceDir , {
404- nativeToolSurfaceEnabled : true ,
405- } ) ;
406- expect ( conversationNativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_exec" ) ;
407- expect ( conversationNativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_process" ) ;
408-
409- params . config = { tools : { deny : [ "apply_patch" ] } } ;
410- const intersected = await buildDynamicToolsForTest ( params , workspaceDir , {
411- nativeToolSurfaceEnabled : false ,
412- } ) ;
413- expect ( intersected . map ( ( tool ) => tool . name ) ) . not . toContain ( "apply_patch" ) ;
414-
415- params . conversationToolPolicy = { } ;
416- params . config = { tools : { deny : [ "exec" , "process" , "write" , "edit" ] } } ;
417- const globalPolicyTools = await buildDynamicToolsForTest ( params , workspaceDir , {
418- nativeToolSurfaceEnabled : false ,
419- } ) ;
420- expect ( globalPolicyTools . map ( ( tool ) => tool . name ) . toSorted ( ) ) . toEqual ( [ "apply_patch" , "read" ] ) ;
421- const globalNativeTools = await buildDynamicToolsForTest ( params , workspaceDir , {
422- nativeToolSurfaceEnabled : true ,
423- } ) ;
424- expect ( globalNativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_exec" ) ;
425- expect ( globalNativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_process" ) ;
426-
427- params . config = {
428- agents : {
429- list : [ { id : "main" , tools : { deny : [ "exec" , "process" , "write" , "edit" ] } } ] ,
430- } ,
431- } ;
432- const agentPolicyTools = await buildDynamicToolsForTest ( params , workspaceDir , {
433- nativeToolSurfaceEnabled : false ,
434- } ) ;
435- expect ( agentPolicyTools . map ( ( tool ) => tool . name ) . toSorted ( ) ) . toEqual ( [ "apply_patch" , "read" ] ) ;
436- const agentNativeTools = await buildDynamicToolsForTest ( params , workspaceDir , {
437- nativeToolSurfaceEnabled : true ,
438- } ) ;
439- expect ( agentNativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_exec" ) ;
440- expect ( agentNativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_process" ) ;
441- } ) ;
392+ const policyDeny = [ "exec" , "process" , "write" , "edit" ] ;
393+ it . each < {
394+ source : string ;
395+ config ?: EmbeddedRunAttemptParams [ "config" ] ;
396+ conversationToolPolicy ?: EmbeddedRunAttemptParams [ "conversationToolPolicy" ] ;
397+ sandboxAgentId ?: string ;
398+ denied : boolean ;
399+ } > ( [
400+ { source : "allowed" , sandboxAgentId : "policy" , denied : false } ,
401+ { source : "conversation" , conversationToolPolicy : { deny : policyDeny } , denied : true } ,
402+ {
403+ source : "intersection" ,
404+ conversationToolPolicy : { deny : policyDeny } ,
405+ config : { tools : { deny : [ "apply_patch" ] } } ,
406+ denied : true ,
407+ } ,
408+ { source : "global" , config : { tools : { deny : policyDeny } } , denied : true } ,
409+ {
410+ source : "execution owner" ,
411+ config : { agents : { entries : { main : { tools : { deny : policyDeny } } } } } ,
412+ denied : true ,
413+ } ,
414+ {
415+ source : "retained owner" ,
416+ config : { agents : { entries : { main : { } , policy : { tools : { deny : policyDeny } } } } } ,
417+ sandboxAgentId : "policy" ,
418+ denied : true ,
419+ } ,
420+ ] ) (
421+ "enforces $source policy when a dynamic write reaches the bridge" ,
422+ async ( { source, config, conversationToolPolicy, sandboxAgentId, denied } ) => {
423+ const workspaceDir = path . join ( tempDir , "workspace" ) ;
424+ await fs . mkdir ( workspaceDir , { recursive : true } ) ;
425+ const targetPath = path . join ( workspaceDir , "policy-write.txt" ) ;
426+ const params = createParams ( path . join ( tempDir , "policy-session.jsonl" ) , workspaceDir ) ;
427+ params . agentId = "main" ;
428+ params . sandboxSessionKey = sandboxAgentId ? "global" : undefined ;
429+ params . sandboxAgentId = sandboxAgentId ;
430+ params . conversationToolPolicy = conversationToolPolicy ;
431+ params . disableTools = false ;
432+ params . runtimePlan = createCodexRuntimePlanFixture ( ) ;
433+ params . config = {
434+ plugins : { enabled : false } ,
435+ agents : config ?. agents ?? { entries : { main : { } , policy : { } } } ,
436+ tools : { ...config ?. tools , fs : { workspaceOnly : true } } ,
437+ } ;
438+ const beforeToolCall = vi . fn ( ( ) => ( { } ) ) ;
439+ initializeGlobalHookRunner (
440+ createMockPluginRegistry ( [ { hookName : "before_tool_call" , handler : beforeToolCall } ] ) ,
441+ ) ;
442+ try {
443+ await bindProductionCodexHostCapabilities ( params ) ;
444+ setOpenClawCodingToolsFactoryForTests ( ( options ) =>
445+ createOpenClawCodingTools ( options ) . filter ( ( tool ) =>
446+ [ "read" , "write" , "edit" , "apply_patch" , "exec" , "process" ] . includes ( tool . name ) ,
447+ ) ,
448+ ) ;
449+ const tools = await buildDynamicToolsForTest ( params , workspaceDir , {
450+ sandboxSessionKey : params . sandboxSessionKey ?? params . sessionKey ,
451+ nativeToolSurfaceEnabled : false ,
452+ sandbox : null ,
453+ } ) ;
454+ const bridge = createCodexDynamicToolBridge ( {
455+ tools,
456+ signal : new AbortController ( ) . signal ,
457+ loading : "direct" ,
458+ hookContext : {
459+ agentId : "main" ,
460+ sessionKey : params . sessionKey ,
461+ sessionId : params . sessionId ,
462+ runId : params . runId ,
463+ workspaceDir,
464+ } ,
465+ } ) ;
466+ const result = await bridge . handleToolCall ( {
467+ threadId : "thread-policy" ,
468+ turnId : "turn-policy" ,
469+ callId : "write-policy" ,
470+ tool : "write" ,
471+ arguments : { path : targetPath , content : "allowed owner write" } ,
472+ } ) ;
473+ if ( denied ) {
474+ await expect ( fs . readFile ( targetPath , "utf8" ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
475+ expect ( result . success ) . toBe ( false ) ;
476+ expect ( beforeToolCall ) . not . toHaveBeenCalled ( ) ;
477+ expect ( tools . map ( ( tool ) => tool . name ) . toSorted ( ) ) . toEqual (
478+ source === "intersection" ? [ "read" ] : [ "apply_patch" , "read" ] ,
479+ ) ;
480+ const nativeTools = await buildDynamicToolsForTest ( params , workspaceDir , {
481+ sandboxSessionKey : params . sandboxSessionKey ?? params . sessionKey ,
482+ } ) ;
483+ expect ( nativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_exec" ) ;
484+ expect ( nativeTools . map ( ( tool ) => tool . name ) ) . not . toContain ( "gateway_process" ) ;
485+ } else {
486+ expect ( result . success ) . toBe ( true ) ;
487+ await expect ( fs . readFile ( targetPath , "utf8" ) ) . resolves . toBe ( "allowed owner write" ) ;
488+ expect ( beforeToolCall ) . toHaveBeenCalledWith (
489+ expect . objectContaining ( { toolName : "write" } ) ,
490+ expect . objectContaining ( { agentId : "main" , sessionKey : params . sessionKey } ) ,
491+ ) ;
492+ }
493+ } finally {
494+ resetGlobalHookRunner ( ) ;
495+ }
496+ } ,
497+ ) ;
442498
443499 it ( "removes account-wide app access when native tools are restricted" , ( ) => {
444500 expect (
@@ -1697,6 +1753,7 @@ describe("Codex app-server dynamic tool build", () => {
16971753 runtimePolicyParams . runtimePlan = createCodexRuntimePlanFixture ( ) ;
16981754 runtimePolicyParams . sessionKey = "agent:main:session-1" ;
16991755 runtimePolicyParams . sandboxSessionKey = "agent:policy:session-1" ;
1756+ runtimePolicyParams . sandboxAgentId = "policy" ;
17001757 runtimePolicyParams . config = {
17011758 agents : {
17021759 entries : {
@@ -1713,7 +1770,7 @@ describe("Codex app-server dynamic tool build", () => {
17131770 const runtimePolicyTools = await buildDynamicToolsForTest ( runtimePolicyParams , workspaceDir , {
17141771 sandboxSessionKey : "agent:policy:session-1" ,
17151772 nativeToolSurfaceEnabled : runtimePolicyNativeToolSurfaceEnabled ,
1716- sessionAgentId : "policy " ,
1773+ sessionAgentId : "main " ,
17171774 } ) ;
17181775
17191776 expect ( runtimePolicyNativeToolSurfaceEnabled ) . toBe ( false ) ;
0 commit comments