1919import io .github .agentic .ai .api .chat .ChatRole ;
2020import io .github .agentic .ai .api .chat .ToolCall ;
2121
22+ import java .util .ArrayList ;
2223import java .util .HashMap ;
23- import java .util .LinkedHashSet ;
2424import java .util .List ;
2525import java .util .Map ;
2626import java .util .Objects ;
2727import java .util .Optional ;
28- import java .util .Set ;
2928
3029/**
3130 * Validates that the tool calls of a conversation and their results stay paired.
3433 * Compression may remove a message. When it removes a tool result, the assistant
3534 * message which requested the tool call is left with an unanswered call; when it
3635 * removes the assistant message, the tool result is left without a call. Both break
37- * the conversation, so the budget gate refuses to dispatch such a request.
36+ * the conversation, so the budget gate refuses to dispatch such a request. The
37+ * validation is position-aware: a result must follow the assistant message which
38+ * requested the call, and neither calls nor results may repeat.
3839 */
3940public final class ToolCallPairingValidator {
4041
@@ -59,7 +60,17 @@ public enum Kind {
5960 /**
6061 * The same tool call appears more than once.
6162 */
62- DUPLICATE_TOOL_CALL
63+ DUPLICATE_TOOL_CALL ,
64+
65+ /**
66+ * The same tool result appears more than once.
67+ */
68+ DUPLICATE_TOOL_RESULT ,
69+
70+ /**
71+ * A tool result appears before the assistant message which requested the call.
72+ */
73+ RESULT_BEFORE_CALL
6374 }
6475
6576 /**
@@ -90,34 +101,46 @@ public record Violation(Kind kind, String toolCallId) {
90101 public static Optional <Violation > validate (List <ChatMessage > messages ) {
91102 Objects .requireNonNull (messages , "messages must not be null" );
92103 Map <String , Integer > callCounts = new HashMap <>();
93- Set <String > calls = new LinkedHashSet <>();
94- for (ChatMessage message : messages ) {
104+ Map <String , Integer > callFirstIndex = new HashMap <>();
105+ Map <String , List <Integer >> resultIndexes = new HashMap <>();
106+ for (int index = 0 ; index < messages .size (); index ++) {
107+ ChatMessage message = messages .get (index );
95108 for (ToolCall call : message .toolCalls ()) {
96109 if (call .id () == null ) {
97110 continue ;
98111 }
99112 callCounts .merge (call .id (), 1 , Integer ::sum );
100- calls .add (call .id ());
113+ callFirstIndex .putIfAbsent (call .id (), index );
114+ }
115+ if (message .role () == ChatRole .TOOL && message .toolCallId () != null ) {
116+ resultIndexes .computeIfAbsent (message .toolCallId (), key -> new ArrayList <>())
117+ .add (index );
101118 }
102119 }
103120 for (Map .Entry <String , Integer > call : callCounts .entrySet ()) {
104121 if (call .getValue () > 1 ) {
105122 return Optional .of (new Violation (Kind .DUPLICATE_TOOL_CALL , call .getKey ()));
106123 }
107124 }
108- Set <String > results = new LinkedHashSet <>();
109- for (ChatMessage message : messages ) {
110- if (message .role () == ChatRole .TOOL && message .toolCallId () != null ) {
111- results .add (message .toolCallId ());
125+ for (Map .Entry <String , List <Integer >> result : resultIndexes .entrySet ()) {
126+ if (result .getValue ().size () > 1 ) {
127+ return Optional .of (new Violation (Kind .DUPLICATE_TOOL_RESULT , result .getKey ()));
112128 }
113129 }
114- for (String result : results ) {
115- if (!calls .contains (result )) {
116- return Optional .of (new Violation (Kind .UNKNOWN_TOOL_RESULT , result ));
130+ for (Map .Entry <String , List <Integer >> result : resultIndexes .entrySet ()) {
131+ Integer callIndex = callFirstIndex .get (result .getKey ());
132+ if (callIndex == null ) {
133+ return Optional .of (new Violation (Kind .UNKNOWN_TOOL_RESULT , result .getKey ()));
134+ }
135+ // A result must follow the assistant message which requested the call:
136+ // a compacted history that moves a result in front of its call would
137+ // otherwise reach the provider.
138+ if (result .getValue ().get (0 ) < callIndex ) {
139+ return Optional .of (new Violation (Kind .RESULT_BEFORE_CALL , result .getKey ()));
117140 }
118141 }
119- for (String call : calls ) {
120- if (!results . contains (call )) {
142+ for (String call : callCounts . keySet () ) {
143+ if (!resultIndexes . containsKey (call )) {
121144 return Optional .of (new Violation (Kind .MISSING_TOOL_RESULT , call ));
122145 }
123146 }
0 commit comments