Add tool-confirmation module: Spring AI 2.1 human-in-the-loop tool calls (requiresConfirmation, ToolCallConfirmationPolicy, ToolCallDecisions pause and resume), a server-side approval store with approver check, replay guard and audit log, and an approver page over SSE and htmx; 17 transcripts. The model is a scripted ChatModel; the htmx page was not opened in a browser. Spring Boot 4.1.3, Spring AI 2.1.0, Java 25.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5x1ufsjyvu9RJFUUb
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# A gated tool pauses the loop; an approval resumes it
|
||||
|
||||
-- first request ----------------------------------------------
|
||||
model calls so far: 1
|
||||
reply text (content()): ''
|
||||
reply has tool calls: true
|
||||
awaitingDecision(reply): [call_1]
|
||||
isAwaitingDecision(reply): true
|
||||
tool call shown to the person: bookFlight {"flightNumber":"VX205"}
|
||||
ledger (real side effects): []
|
||||
metadata key holding the keys: spring.ai.tool-calls-awaiting-decision
|
||||
|
||||
-- after allow(call_1) + executeToolCalls ---------------------
|
||||
ledger (real side effects): [bookFlight VX205]
|
||||
conversationHistory():
|
||||
UserMessage[Book flight VX205 for tomorrow]
|
||||
AssistantMessage[toolCalls=call_1:bookFlight{"flightNumber":"VX205"}]
|
||||
ToolResponseMessage[call_1:bookFlight -> "Booked VX205, reference BK-5"]
|
||||
returnDirect(): false
|
||||
|
||||
-- resumed request --------------------------------------------
|
||||
model calls so far: 2
|
||||
final answer: Done. VX205 is booked.
|
||||
@@ -0,0 +1,13 @@
|
||||
# One gated call pauses the whole reply, including the harmless call beside it
|
||||
|
||||
calls in the reply: [call_1=searchFlights, call_2=bookFlight]
|
||||
awaitingDecision(reply): [call_2]
|
||||
ledger after the pause: []
|
||||
|
||||
decision for call_2 only -> IllegalArgumentException:
|
||||
Every tool call of the reply needs a decision; missing: [call_1]
|
||||
ledger after the refused resume: []
|
||||
|
||||
decisions for both -> ledger: [searchFlights BOS->SFO, bookFlight VX205]
|
||||
tool results, in the model's order:
|
||||
ToolResponseMessage[call_1:searchFlights -> "VX205 07:10 USD 240; VX311 18:40 USD 198"][call_2:bookFlight -> "Booked VX205, reference BK-5"]
|
||||
@@ -0,0 +1,12 @@
|
||||
# What the model receives when a call is denied
|
||||
|
||||
DEFAULT_DENY_MESSAGE: The user did not allow this tool call.
|
||||
|
||||
history the model is sent next:
|
||||
UserMessage[Rebook me on VX205]
|
||||
AssistantMessage[toolCalls=call_1:bookFlight{"flightNumber":"VX205"}, call_2:cancelBooking{"reference":"BK-9"}]
|
||||
ToolResponseMessage[call_1:bookFlight -> The user did not allow this tool call.][call_2:cancelBooking -> The user wants an aisle seat, ask again.]
|
||||
|
||||
ledger (nothing ran): []
|
||||
returnDirect(): false
|
||||
model's next answer: Understood, I will ask which seat you prefer.
|
||||
@@ -0,0 +1,13 @@
|
||||
# Which calls pause under which policy
|
||||
|
||||
policy | call result
|
||||
default (fromToolMetadata) | bookFlight PAUSES [c1]
|
||||
default (fromToolMetadata) | transferMoney 500 runs (ledger [transferMoney ACME 500])
|
||||
none() | bookFlight runs (ledger [bookFlight VX205])
|
||||
forTools("transferMoney") | transferMoney 500 PAUSES [c2]
|
||||
forTools("transferMoney") | bookFlight runs (ledger [bookFlight VX205])
|
||||
over-1000 argument policy | transferMoney 500 runs (ledger [transferMoney ACME 500])
|
||||
over-1000 argument policy | transferMoney 5000 PAUSES [c3]
|
||||
over-1000 argument policy | bookFlight runs (ledger [bookFlight VX205])
|
||||
fromToolMetadata().or(over-1000)| bookFlight PAUSES [c1]
|
||||
fromToolMetadata().or(over-1000)| transferMoney 5000 PAUSES [c3]
|
||||
@@ -0,0 +1,10 @@
|
||||
# Three refused resumes
|
||||
|
||||
1. no decision for call_1:
|
||||
IllegalArgumentException: Every tool call of the reply needs a decision; missing: [call_1]
|
||||
2. a decision for a key that is not in the reply (call_99):
|
||||
IllegalArgumentException: No tool call has the key 'call_99'; the reply's tool call keys: [call_1]
|
||||
3. allowed call, but its tool is not on the Prompt:
|
||||
IllegalArgumentException: The prompt does not carry the tool 'bookFlight' of the allowed call 'call_1'; an allowed call runs only with a tool of the prompt
|
||||
|
||||
ledger after all three: []
|
||||
@@ -0,0 +1,5 @@
|
||||
# executeToolCalls twice with the same decisions on the same reply
|
||||
|
||||
calls to executeToolCalls: 2
|
||||
ledger: [bookFlight VX205, bookFlight VX205]
|
||||
A customer is booked, and charged, twice.
|
||||
@@ -0,0 +1,19 @@
|
||||
# Search (ungated) then book (gated) in one turn: what the model sees on each call
|
||||
|
||||
ledger at the pause: [searchFlights BOS->SFO]
|
||||
model calls: 3
|
||||
|
||||
prompt 1 sent to the model:
|
||||
UserMessage[Find a flight BOS to SFO and book the first one]
|
||||
|
||||
prompt 2 sent to the model:
|
||||
UserMessage[Find a flight BOS to SFO and book the first one]
|
||||
AssistantMessage[toolCalls=s1:searchFlights{"from":"BOS","to":"SFO"}]
|
||||
ToolResponseMessage[s1:searchFlights -> "VX205 07:10 USD 240; VX311 18:40 USD 198"]
|
||||
|
||||
prompt 3 sent to the model:
|
||||
UserMessage[Find a flight BOS to SFO and book the first one]
|
||||
AssistantMessage[toolCalls=b1:bookFlight{"flightNumber":"VX205"}]
|
||||
ToolResponseMessage[b1:bookFlight -> "Booked VX205, reference BK-5"]
|
||||
|
||||
Prompt 2 had the search round; prompt 3 (the resume) does not.
|
||||
@@ -0,0 +1,5 @@
|
||||
# spring.ai.tools.confirmation.tools=transferMoney (a USD 5 transfer, far under the 1000 policy bean)
|
||||
|
||||
status: AWAITING_APPROVAL
|
||||
calls: [CallView[key=t1, name=transferMoney, arguments={"payee":"ACME","amount":5}]]
|
||||
ledger: []
|
||||
@@ -0,0 +1,6 @@
|
||||
# spring.ai.tools.confirmation.enabled=false
|
||||
|
||||
startup warning: WARN ToolCallingAutoConfiguration: Tool call confirmation is disabled (spring.ai.tools.confirmation.enabled=false): tools marked requiresConfirmation run as soon as the model calls them, without a person's confirmation.
|
||||
|
||||
bookFlight (requiresConfirmation = true): status=DONE ledger=[bookFlight VX205]
|
||||
transferMoney 5000 (policy bean): status=AWAITING_APPROVAL ledger=[bookFlight VX205]
|
||||
@@ -0,0 +1,27 @@
|
||||
# The approval API over HTTP
|
||||
|
||||
POST /api/chat (carol: Book VX205)
|
||||
-> 200 {"status":"AWAITING_APPROVAL","answer":null,"approvalId":"4dbe1483","calls":[{"key":"call_1","name":"bookFlight","arguments":"{\"flightNumber\":\"VX205\"}"}]}
|
||||
ledger: []
|
||||
|
||||
GET /api/approvals
|
||||
-> 200 [{"id":"4dbe1483","conversationId":"c1","requestedBy":"carol","calls":[{"key":"call_1","name":"bookFlight","arguments":"{\"flightNumber\":\"VX205\"}"}]}]
|
||||
|
||||
POST /api/approvals/4dbe1483/decisions X-Approver: mallory (allow call_1)
|
||||
-> 403 {"error":"'mallory' may not approve"}
|
||||
ledger: []
|
||||
|
||||
POST /api/approvals/4dbe1483/decisions X-Approver: alice (no decisions)
|
||||
-> 400 {"error":"Every tool call of the reply needs a decision; missing: [call_1]"}
|
||||
still pending: true
|
||||
|
||||
POST /api/approvals/4dbe1483/decisions X-Approver: alice (allow call_1)
|
||||
-> 200 {"status":"DONE","answer":"Booked VX205. Reference BK-5.","approvalId":null,"calls":[]}
|
||||
ledger: [bookFlight VX205]
|
||||
|
||||
POST /api/approvals/4dbe1483/decisions X-Approver: bob (the same pause again)
|
||||
-> 409 {"error":"approval 4dbe1483 was already resolved"}
|
||||
ledger: [bookFlight VX205]
|
||||
|
||||
POST /api/approvals/nope0000/decisions X-Approver: alice
|
||||
-> 404 {"error":"no such approval nope0000"}
|
||||
@@ -0,0 +1,7 @@
|
||||
# What the audit log recorded for that exchange
|
||||
|
||||
PAUSED approval=4dbe1483 actor=carol tool=bookFlight {"flightNumber":"VX205"}
|
||||
APPROVER_REFUSED approval=4dbe1483 actor=mallory tool=- not in the approver list
|
||||
DECISION_REJECTED approval=4dbe1483 actor=alice tool=- Every tool call of the reply needs a decision; missing: [call_1]
|
||||
APPROVED approval=4dbe1483 actor=alice tool=bookFlight {"flightNumber":"VX205"}
|
||||
REPLAY_REFUSED approval=4dbe1483 actor=bob tool=- pause already resolved
|
||||
@@ -0,0 +1,5 @@
|
||||
# 16 threads approve the same pause at once
|
||||
|
||||
1 x 200 DONE
|
||||
15 x 409 approval <id> was already resolved
|
||||
ledger: [bookFlight VX205]
|
||||
@@ -0,0 +1,23 @@
|
||||
# Approver page: SSE events and an escaped fragment (injected tool argument)
|
||||
|
||||
tool argument chosen by the model: <img src=x onerror=alert(1)>
|
||||
|
||||
SSE lines the approver page received:
|
||||
event:hello
|
||||
data:connected
|
||||
event:approval-requested
|
||||
data:69b1d486
|
||||
event:approval-resolved
|
||||
data:69b1d486
|
||||
|
||||
GET /ui/approvals (the <pre> element):
|
||||
<pre>{"flightNumber":"<img src=x onerror=alert(1)>"}</pre>
|
||||
|
||||
POST /ui/approvals/69b1d486 (approver=alice, deny) ->
|
||||
<p class="ok">Decision recorded.</p>
|
||||
ledger: []
|
||||
|
||||
approver.html wiring:
|
||||
<!-- The page opens ONE SSE connection. Each event re-runs the hx-get below; the browser never polls. -->
|
||||
<body hx-ext="sse" sse-connect="/approvals/stream">
|
||||
<div id="pending" hx-get="/ui/approvals" hx-trigger="load, sse:approval-requested, sse:approval-resolved"></div>
|
||||
@@ -0,0 +1,5 @@
|
||||
# ctx.close() with one approver page connected (grace period 5000 ms)
|
||||
|
||||
streams left open: over 4000 ms
|
||||
streams completed by the lifecycle: under 2000 ms
|
||||
(the exact figures vary per run; the 5 s wall is the configured grace period)
|
||||
@@ -0,0 +1,63 @@
|
||||
# Public API of the confirmation classes, from spring-ai-model-2.1.0.jar (javap -public)
|
||||
|
||||
public interface org.springframework.ai.model.tool.ToolCallConfirmationPolicy {
|
||||
public abstract boolean requiresConfirmation(org.springframework.ai.chat.messages.AssistantMessage$ToolCall, org.springframework.ai.tool.metadata.ToolMetadata);
|
||||
public default org.springframework.ai.model.tool.ToolCallConfirmationPolicy or(org.springframework.ai.model.tool.ToolCallConfirmationPolicy);
|
||||
public static org.springframework.ai.model.tool.ToolCallConfirmationPolicy none();
|
||||
public static org.springframework.ai.model.tool.ToolCallConfirmationPolicy fromToolMetadata();
|
||||
public static org.springframework.ai.model.tool.ToolCallConfirmationPolicy forTools(java.lang.String...);
|
||||
}
|
||||
|
||||
public class org.springframework.ai.model.tool.ToolCallConfirmationRequiredException extends java.lang.RuntimeException {
|
||||
public org.springframework.ai.model.tool.ToolCallConfirmationRequiredException(java.util.List<java.lang.String>);
|
||||
public java.util.List<java.lang.String> getToolCallIds();
|
||||
}
|
||||
|
||||
public final class org.springframework.ai.model.tool.ToolCallDecision extends java.lang.Record {
|
||||
public static final java.lang.String DEFAULT_DENY_MESSAGE;
|
||||
public org.springframework.ai.model.tool.ToolCallDecision(java.lang.String, boolean, java.lang.String);
|
||||
public static org.springframework.ai.model.tool.ToolCallDecision allow(java.lang.String);
|
||||
public static org.springframework.ai.model.tool.ToolCallDecision deny(java.lang.String);
|
||||
public static org.springframework.ai.model.tool.ToolCallDecision deny(java.lang.String, java.lang.String);
|
||||
public java.lang.String denyMessage();
|
||||
public final java.lang.String toString();
|
||||
public final int hashCode();
|
||||
public final boolean equals(java.lang.Object);
|
||||
public java.lang.String toolCallId();
|
||||
public boolean allowed();
|
||||
public java.lang.String message();
|
||||
}
|
||||
|
||||
public final class org.springframework.ai.model.tool.ToolCallDecisions extends java.lang.Record {
|
||||
public static final java.lang.String POSITION_KEY_PREFIX;
|
||||
public static final java.lang.String AWAITING_DECISION_METADATA_KEY;
|
||||
public org.springframework.ai.model.tool.ToolCallDecisions(java.util.List<org.springframework.ai.model.tool.ToolCallDecision>);
|
||||
public static org.springframework.ai.model.tool.ToolCallDecisions of(org.springframework.ai.model.tool.ToolCallDecision...);
|
||||
public org.springframework.ai.model.tool.ToolCallDecision decisionOn(java.lang.String);
|
||||
public static java.util.List<java.lang.String> toolCallKeys(org.springframework.ai.chat.messages.AssistantMessage);
|
||||
public static java.util.List<java.lang.String> awaitingDecision(org.springframework.ai.chat.model.ChatResponse);
|
||||
public static boolean isAwaitingDecision(org.springframework.ai.chat.model.ChatResponse);
|
||||
public final java.lang.String toString();
|
||||
public final int hashCode();
|
||||
public final boolean equals(java.lang.Object);
|
||||
public java.util.List<org.springframework.ai.model.tool.ToolCallDecision> decisions();
|
||||
}
|
||||
|
||||
public interface org.springframework.ai.tool.metadata.ToolMetadata {
|
||||
public default boolean returnDirect();
|
||||
public default boolean requiresConfirmation();
|
||||
public static org.springframework.ai.tool.metadata.DefaultToolMetadata$Builder builder();
|
||||
public static org.springframework.ai.tool.metadata.ToolMetadata from(java.lang.reflect.Method);
|
||||
}
|
||||
|
||||
# ToolCallingManager (the overload resumption uses is a default method)
|
||||
public interface org.springframework.ai.model.tool.ToolCallingManager {
|
||||
public abstract java.util.List<org.springframework.ai.tool.definition.ToolDefinition> resolveToolDefinitions(org.springframework.ai.model.tool.ToolCallingChatOptions);
|
||||
public abstract org.springframework.ai.model.tool.ToolExecutionResult executeToolCalls(org.springframework.ai.chat.prompt.Prompt, org.springframework.ai.chat.model.ChatResponse);
|
||||
public default org.springframework.ai.model.tool.ToolExecutionResult executeToolCalls(org.springframework.ai.chat.prompt.Prompt, org.springframework.ai.chat.model.ChatResponse, org.springframework.ai.model.tool.ToolCallDecisions);
|
||||
public static org.springframework.ai.model.tool.DefaultToolCallingManager$Builder builder();
|
||||
}
|
||||
|
||||
# Configuration properties, from spring-ai-autoconfigure-model-tool-2.1.0.jar
|
||||
spring.ai.tools.confirmation.enabled | default: True
|
||||
spring.ai.tools.confirmation.tools | default: None
|
||||
@@ -0,0 +1,7 @@
|
||||
# Tool call keys when ids are missing or repeated
|
||||
|
||||
POSITION_KEY_PREFIX: #
|
||||
two calls that both have id 'dup' -> keys: [#0, #1]
|
||||
awaitingDecision for that reply: [#0, #1]
|
||||
one call with an empty id -> keys: [#0]
|
||||
allow('#0') + deny('#1') -> ledger: [bookFlight VX205]
|
||||
@@ -0,0 +1,5 @@
|
||||
# A ToolCallingManager that wraps the default and forgets the decision overload
|
||||
|
||||
pause through the wrapping manager: awaitingDecision = [call_1]
|
||||
resume through the wrapping manager: UnsupportedOperationException: com.ankurm.toolconfirmation.CustomManagerTest$ForgetfulManager does not support tool call decisions
|
||||
ledger: []
|
||||
Reference in New Issue
Block a user