Skip to content

Commit a2b92f5

Browse files
authored
fix(agent): keep interruption feedback until it can be applied (Fixes agentic-ai-java#211) (agentic-ai-java#222)
1 parent f503a2e commit a2b92f5

2 files changed

Lines changed: 135 additions & 9 deletions

File tree

‎argi-agent-framework/src/main/java/io/github/agentic/ai/graph/agent/hook/InterruptionHook.java‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,9 @@ public CompletableFuture<Map<String, Object>> apply(OverAllState state, Runnable
7070
return CompletableFuture.completedFuture(Map.of());
7171
}
7272

73-
// Atomically get and remove the feedback key using remove()
74-
// ConcurrentHashMap.remove() is thread-safe and returns the removed value
75-
// This ensures thread-safety: if updateAgentState() is called concurrently,
76-
// it will either see the old value (before removal) or set a new value (after
77-
// removal)
78-
// The remove() operation is atomic, preventing race conditions
79-
Object feedback = agentThreadState.remove(INTERRUPTION_FEEDBACK_KEY);
73+
// Read the feedback without consuming it: it is only claimed once it can be
74+
// applied, so a pass that cannot apply it leaves it pending for a later pass.
75+
Object feedback = agentThreadState.get(INTERRUPTION_FEEDBACK_KEY);
8076

8177
if (feedback == null) {
8278
log.debug("No interruption feedback found in state, continue reasoning with no updates.");
@@ -100,6 +96,7 @@ public CompletableFuture<Map<String, Object>> apply(OverAllState state, Runnable
10096
if (feedbackMessages.isEmpty()) {
10197
log.warn(
10298
"Feedback list is empty or contains no valid ChatMessage instances, stop and wait for more input.");
99+
claim(agentThreadState, feedback);
103100
return CompletableFuture.completedFuture(Map.of());
104101
}
105102
}
@@ -111,8 +108,9 @@ else if (feedback instanceof String) {
111108
}
112109
else {
113110
log.warn(
114-
"Interruption feedback is neither List<ChatMessage>, ChatMessage nor String, stop and wait for more input. Type: {}",
111+
"Interruption feedback is neither List<ChatMessage>, ChatMessage nor String, discarding it. Type: {}",
115112
feedback.getClass().getName());
113+
claim(agentThreadState, feedback);
116114
return CompletableFuture.completedFuture(Map.of());
117115
}
118116

@@ -123,12 +121,21 @@ else if (feedback instanceof String) {
123121
if (!messages.isEmpty() && messages.get(messages.size() - 1).role() == ChatRole.ASSISTANT) {
124122
ChatMessage assistantMessage = messages.get(messages.size() - 1);
125123
if (assistantMessage.hasToolCalls()) {
124+
// Feedback cannot be placed between tool calls and their results; keep it
125+
// pending until the next model pass.
126126
log.info(
127-
"Last message is an ChatMessage with tool calls, not adding interruption feedback to messages list.");
127+
"Last message is an ChatMessage with tool calls, keeping interruption feedback pending for the next model pass.");
128128
return CompletableFuture.completedFuture(Map.of());
129129
}
130130
}
131131

132+
if (!claim(agentThreadState, feedback)) {
133+
// Newer feedback replaced this value after it was read; leave the newer
134+
// value for the next pass instead of applying the superseded one.
135+
log.debug("Interruption feedback was replaced concurrently, deferring to the newer feedback.");
136+
return CompletableFuture.completedFuture(Map.of());
137+
}
138+
132139
// Create new messages list with the feedback messages appended
133140
List<ChatMessage> newMessages = new ArrayList<>(feedbackMessages);
134141

@@ -142,6 +149,14 @@ else if (feedback instanceof String) {
142149
return CompletableFuture.completedFuture(updates);
143150
}
144151

152+
/**
153+
* Removes the feedback only if it is still the value that was read, so feedback
154+
* written concurrently by {@code updateAgentState} is never deleted unseen.
155+
*/
156+
private static boolean claim(Map<String, Object> agentThreadState, Object feedback) {
157+
return agentThreadState.remove(INTERRUPTION_FEEDBACK_KEY, feedback);
158+
}
159+
145160
@Override
146161
public Optional<InterruptionMetadata> interrupt(String nodeId, OverAllState state, RunnableConfig config) {
147162
// Check INTERRUPTION_FEEDBACK_KEY from state

‎argi-agent-framework/src/test/java/io/github/agentic/ai/graph/agent/hooks/InterruptionHookTest.java‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)