@@ -351,6 +351,117 @@ public void testApply_EmptyMessagesList_ShouldAddMessage() throws Exception {
351351 "INTERRUPTION_FEEDBACK_KEY should be removed from agent thread state" );
352352 }
353353
354+ @ Test
355+ public void testApply_PendingToolCalls_KeepsFeedbackForNextPass () throws Exception {
356+ String threadId = "test-thread-pending-tool-calls" ;
357+ RunnableConfig config = RunnableConfig .builder ().threadId (threadId ).build ();
358+ mockAgent .updateAgentState ("Please also check the weather" , config );
359+ ChatMessage toolCalling = ChatMessage .assistant ("" ,
360+ List .of (new io .github .agentic .ai .api .chat .ToolCall ("call-1" , "poem" , "{}" )));
361+ OverAllState pendingTools = new OverAllState (
362+ Map .of ("messages" , List .of (ChatMessage .user ("Initial message" ), toolCalling )));
363+
364+ Map <String , Object > deferred = hook .apply (pendingTools , config ).get ();
365+
366+ Assertions .assertTrue (deferred .isEmpty (), "Feedback must not be applied while tool calls are pending" );
367+ Assertions .assertEquals ("Please also check the weather" ,
368+ mockAgent .getThreadState (threadId ).get (InterruptionHook .INTERRUPTION_FEEDBACK_KEY ),
369+ "Feedback must stay pending when it cannot be applied" );
370+
371+ OverAllState afterTools = new OverAllState (Map .of ("messages" , List .of (ChatMessage .user ("Initial message" ),
372+ toolCalling , ChatMessage .tool ("call-1" , "poem" , "a poem" ))));
373+ Map <String , Object > applied = hook .apply (afterTools , config ).get ();
374+
375+ @ SuppressWarnings ("unchecked" )
376+ List <ChatMessage > messages = (List <ChatMessage >) applied .get ("messages" );
377+ Assertions .assertEquals (1 , messages .size ());
378+ Assertions .assertEquals (ChatRole .USER , messages .get (0 ).role ());
379+ Assertions .assertEquals ("Please also check the weather" , messages .get (0 ).text ());
380+ Assertions .assertNull (mockAgent .getThreadState (threadId ).get (InterruptionHook .INTERRUPTION_FEEDBACK_KEY ));
381+
382+ Assertions .assertTrue (hook .apply (afterTools , config ).get ().isEmpty (),
383+ "Consumed feedback must not be applied twice" );
384+ }
385+
386+ @ Test
387+ public void testApply_InvalidFeedbackWithPendingToolCalls_IsDiscarded () throws Exception {
388+ String threadId = "test-thread-invalid-pending-tool-calls" ;
389+ RunnableConfig config = RunnableConfig .builder ().threadId (threadId ).build ();
390+ mockAgent .updateAgentState (123 , config );
391+ ChatMessage toolCalling = ChatMessage .assistant ("" ,
392+ List .of (new io .github .agentic .ai .api .chat .ToolCall ("call-1" , "poem" , "{}" )));
393+ OverAllState pendingTools = new OverAllState (Map .of ("messages" , List .of (toolCalling )));
394+
395+ Assertions .assertTrue (hook .apply (pendingTools , config ).get ().isEmpty ());
396+ Assertions .assertNull (mockAgent .getThreadState (threadId ).get (InterruptionHook .INTERRUPTION_FEEDBACK_KEY ),
397+ "Invalid feedback is discarded rather than replayed forever" );
398+ }
399+
400+ @ Test
401+ public void testApply_ConcurrentNewFeedback_IsNotRemovedByOlderClaim () throws Exception {
402+ String threadId = "test-thread-concurrent-feedback" ;
403+ RunnableConfig config = RunnableConfig .builder ().threadId (threadId ).build ();
404+ CountDownLatch read = new CountDownLatch (1 );
405+ CountDownLatch replaced = new CountDownLatch (1 );
406+ // The feedback list signals the read, then waits until newer feedback is written
407+ // before the hook tries to claim it.
408+ List <ChatMessage > older = new java .util .AbstractList <>() {
409+ private final List <ChatMessage > delegate = List .of (ChatMessage .user ("older" ));
410+
411+ @ Override
412+ public ChatMessage get (int index ) {
413+ return delegate .get (index );
414+ }
415+
416+ @ Override
417+ public int size () {
418+ return delegate .size ();
419+ }
420+
421+ @ Override
422+ public java .util .Iterator <ChatMessage > iterator () {
423+ read .countDown ();
424+ try {
425+ Assertions .assertTrue (replaced .await (5 , TimeUnit .SECONDS ));
426+ }
427+ catch (InterruptedException ex ) {
428+ Thread .currentThread ().interrupt ();
429+ throw new IllegalStateException (ex );
430+ }
431+ return delegate .iterator ();
432+ }
433+
434+ @ Override
435+ public boolean equals (Object other ) {
436+ return this == other ;
437+ }
438+
439+ @ Override
440+ public int hashCode () {
441+ return System .identityHashCode (this );
442+ }
443+ };
444+ mockAgent .updateAgentState (older , config );
445+
446+ CompletableFuture <Map <String , Object >> pending = CompletableFuture
447+ .supplyAsync (() -> hook .apply (state , config ).join ());
448+ Assertions .assertTrue (read .await (5 , TimeUnit .SECONDS ));
449+ mockAgent .updateAgentState ("newer" , config );
450+ replaced .countDown ();
451+ Map <String , Object > first = pending .get (5 , TimeUnit .SECONDS );
452+
453+ Assertions .assertTrue (first .isEmpty (), "Superseded feedback must not be applied" );
454+ Assertions .assertEquals ("newer" ,
455+ mockAgent .getThreadState (threadId ).get (InterruptionHook .INTERRUPTION_FEEDBACK_KEY ),
456+ "Newer feedback must survive the older claim" );
457+
458+ Map <String , Object > second = hook .apply (state , config ).get ();
459+ @ SuppressWarnings ("unchecked" )
460+ List <ChatMessage > messages = (List <ChatMessage >) second .get ("messages" );
461+ Assertions .assertEquals ("newer" , messages .get (0 ).text ());
462+ Assertions .assertNull (mockAgent .getThreadState (threadId ).get (InterruptionHook .INTERRUPTION_FEEDBACK_KEY ));
463+ }
464+
354465 @ Test
355466 public void testGetName () {
356467 // When: getName is called
0 commit comments