11// Codex tests cover dynamic tool execution plugin behavior.
2- import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime" ;
2+ import {
3+ embeddedAgentLog ,
4+ type EmbeddedRunAttemptParams ,
5+ } from "openclaw/plugin-sdk/agent-harness-runtime" ;
36import { afterEach , describe , expect , it , vi } from "vitest" ;
47import {
58 handleDynamicToolCallWithTimeout ,
@@ -311,6 +314,10 @@ describe("dynamic tool execution helpers", () => {
311314 signal : new AbortController ( ) . signal ,
312315 timeoutMs : 1 ,
313316 onAgentToolResult,
317+ observeToolTerminal : ( ) => ( {
318+ executionStarted : true ,
319+ sideEffectEvidence : true ,
320+ } ) ,
314321 onFallbackSelected,
315322 onTimeout,
316323 } ) ;
@@ -327,6 +334,7 @@ describe("dynamic tool execution helpers", () => {
327334 ] ,
328335 } ) ;
329336 expect ( ( await response ) . diagnosticTerminalReason ) . toBe ( "timed_out" ) ;
337+ expect ( ( await response ) . executionStarted ) . toBe ( true ) ;
330338 expect ( capturedSignal ?. aborted ) . toBe ( true ) ;
331339 expect ( onFallbackSelected ) . toHaveBeenCalledOnce ( ) ;
332340 expect ( onTimeout ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -348,6 +356,126 @@ describe("dynamic tool execution helpers", () => {
348356 } ) ;
349357 } ) ;
350358
359+ it ( "marks a timeout during pre-execution hooks as unstarted" , async ( ) => {
360+ vi . useFakeTimers ( ) ;
361+ const observeToolTerminal = vi . fn ( ( ) => ( {
362+ executionStarted : false ,
363+ sideEffectEvidence : false ,
364+ } ) ) ;
365+ const response = handleDynamicToolCallWithTimeout ( {
366+ call : {
367+ threadId : "thread-1" ,
368+ turnId : "turn-1" ,
369+ callId : "call-prehook-timeout" ,
370+ namespace : null ,
371+ tool : "message" ,
372+ arguments : { action : "send" , text : "hello" } ,
373+ } ,
374+ toolBridge : { handleToolCall : vi . fn ( ( ) => new Promise < never > ( ( ) => { } ) ) } ,
375+ signal : new AbortController ( ) . signal ,
376+ timeoutMs : 1 ,
377+ observeToolTerminal,
378+ } ) ;
379+
380+ await vi . advanceTimersByTimeAsync ( 1 ) ;
381+
382+ await expect ( response ) . resolves . toMatchObject ( { executionStarted : false , success : false } ) ;
383+ expect ( ( await response ) . sideEffectEvidence ) . toBeUndefined ( ) ;
384+ expect ( observeToolTerminal ) . toHaveBeenCalledWith (
385+ expect . objectContaining ( {
386+ toolCallId : "call-prehook-timeout" ,
387+ toolName : "message" ,
388+ outcome : "failure" ,
389+ } ) ,
390+ ) ;
391+ } ) ;
392+
393+ it ( "delegates an unpublished abort boundary to the terminal observer" , async ( ) => {
394+ vi . useFakeTimers ( ) ;
395+ const observeToolTerminal = vi . fn (
396+ (
397+ _observation : Parameters < NonNullable < EmbeddedRunAttemptParams [ "observeToolTerminal" ] > > [ 0 ] ,
398+ ) => ( {
399+ executionStarted : false ,
400+ executedArguments : {
401+ action : "send" ,
402+ target : "channel:adjusted" ,
403+ text : "hello" ,
404+ } ,
405+ sideEffectEvidence : false ,
406+ } ) ,
407+ ) ;
408+ const response = handleDynamicToolCallWithTimeout ( {
409+ call : {
410+ threadId : "thread-1" ,
411+ turnId : "turn-1" ,
412+ callId : "call-abort-aware-timeout" ,
413+ namespace : null ,
414+ tool : "message" ,
415+ arguments : { action : "send" , target : "channel:original" , text : "hello" } ,
416+ } ,
417+ toolBridge : {
418+ handleToolCall : vi . fn ( ( _call , options ) => {
419+ expect ( options ?. retainExecutionSnapshot ) . toBe ( true ) ;
420+ return new Promise < never > ( ( _resolve , reject ) => {
421+ options ?. signal ?. addEventListener (
422+ "abort" ,
423+ ( ) => {
424+ const reason = options . signal ?. reason ;
425+ reject ( reason instanceof Error ? reason : new Error ( "tool call aborted" ) ) ;
426+ } ,
427+ { once : true } ,
428+ ) ;
429+ } ) ;
430+ } ) ,
431+ consumeToolExecutionSnapshot : vi . fn ( ( ) => undefined ) ,
432+ } ,
433+ signal : new AbortController ( ) . signal ,
434+ timeoutMs : 1 ,
435+ observeToolTerminal,
436+ } ) ;
437+
438+ await vi . advanceTimersByTimeAsync ( 1 ) ;
439+
440+ await expect ( response ) . resolves . toMatchObject ( { executionStarted : false , success : false } ) ;
441+ expect ( observeToolTerminal ) . toHaveBeenCalledWith (
442+ expect . objectContaining ( {
443+ arguments : { action : "send" , target : "channel:original" , text : "hello" } ,
444+ outcome : "failure" ,
445+ } ) ,
446+ ) ;
447+ expect ( observeToolTerminal . mock . calls [ 0 ] ?. [ 0 ] ) . not . toHaveProperty ( "executionStarted" ) ;
448+ await expect ( response ) . resolves . toMatchObject ( {
449+ executedArguments : {
450+ action : "send" ,
451+ target : "channel:adjusted" ,
452+ text : "hello" ,
453+ } ,
454+ } ) ;
455+ } ) ;
456+
457+ it ( "uses a conservative dispatched fallback without a terminal observer" , async ( ) => {
458+ vi . useFakeTimers ( ) ;
459+ const response = handleDynamicToolCallWithTimeout ( {
460+ call : {
461+ threadId : "thread-1" ,
462+ turnId : "turn-1" ,
463+ callId : "call-untracked-timeout" ,
464+ namespace : null ,
465+ tool : "custom_mutation" ,
466+ arguments : { } ,
467+ } ,
468+ toolBridge : { handleToolCall : vi . fn ( ( ) => new Promise < never > ( ( ) => { } ) ) } ,
469+ signal : new AbortController ( ) . signal ,
470+ timeoutMs : 1 ,
471+ } ) ;
472+
473+ await vi . advanceTimersByTimeAsync ( 1 ) ;
474+
475+ await expect ( response ) . resolves . toMatchObject ( { executionStarted : true , success : false } ) ;
476+ expect ( ( await response ) . sideEffectEvidence ) . toBe ( true ) ;
477+ } ) ;
478+
351479 it ( "lets a structured sessions_send timeout win after setup work" , async ( ) => {
352480 vi . useFakeTimers ( ) ;
353481 const call = {
@@ -420,6 +548,7 @@ describe("dynamic tool execution helpers", () => {
420548 ] ,
421549 } ) ;
422550 expect ( result . diagnosticTerminalReason ) . toBe ( "cancelled" ) ;
551+ expect ( result . executionStarted ) . toBe ( false ) ;
423552 expect ( handleToolCall ) . not . toHaveBeenCalled ( ) ;
424553 expect ( onAgentToolResult ) . toHaveBeenCalledOnce ( ) ;
425554 expect ( onAgentToolResult ) . toHaveBeenCalledWith ( {
@@ -626,6 +755,11 @@ describe("dynamic tool execution helpers", () => {
626755 } ,
627756 signal : new AbortController ( ) . signal ,
628757 timeoutMs : 1 ,
758+ observeToolTerminal : ( ) => ( {
759+ executionStarted : true ,
760+ executedArguments : { action : "poll" , sessionId : "adjusted-session" } ,
761+ sideEffectEvidence : true ,
762+ } ) ,
629763 } ) ;
630764
631765 await vi . advanceTimersByTimeAsync ( 1 ) ;
@@ -639,6 +773,10 @@ describe("dynamic tool execution helpers", () => {
639773 } ,
640774 ] ,
641775 } ) ;
776+ await expect ( response ) . resolves . toMatchObject ( { executionStarted : true } ) ;
777+ await expect ( response ) . resolves . toMatchObject ( {
778+ executedArguments : { action : "poll" , sessionId : "adjusted-session" } ,
779+ } ) ;
642780 expect ( warn ) . toHaveBeenCalledWith ( "codex dynamic tool call timed out" , {
643781 tool : "process" ,
644782 toolCallId : "call-timeout" ,
@@ -704,6 +842,18 @@ describe("dynamic tool execution helpers", () => {
704842 enumerable : false ,
705843 value : true ,
706844 } ) ;
845+ Object . defineProperties ( response , {
846+ executedArguments : {
847+ configurable : true ,
848+ enumerable : false ,
849+ value : { action : "send" , to : "channel:123" } ,
850+ } ,
851+ executionStarted : {
852+ configurable : true ,
853+ enumerable : false ,
854+ value : true ,
855+ } ,
856+ } ) ;
707857
708858 const protocolResponse = toCodexDynamicToolProtocolResponse ( response ) ;
709859 const progressResponse = toCodexDynamicToolProgressResponse ( response , protocolResponse ) ;
@@ -713,6 +863,8 @@ describe("dynamic tool execution helpers", () => {
713863 success : true ,
714864 } ) ;
715865 expect ( Object . keys ( protocolResponse ) ) . not . toContain ( "asyncStarted" ) ;
866+ expect ( "executionStarted" in protocolResponse ) . toBe ( false ) ;
867+ expect ( "executedArguments" in protocolResponse ) . toBe ( false ) ;
716868 expect ( progressResponse ) . toEqual ( {
717869 contentItems : [ { type : "inputText" , text : "Background task started." } ] ,
718870 details : { async : true , status : "started" } ,
0 commit comments