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; 14 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:
Claude
2026-11-24 05:55:38 +05:30
parent 7612fca082
commit 44cdfa7d2d
62 changed files with 2684 additions and 0 deletions
@@ -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.
+13
View File
@@ -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]
+27
View File
@@ -0,0 +1,27 @@
# The approval API over HTTP
POST /api/chat (carol: Book VX205)
-> 200 {"status":"AWAITING_APPROVAL","answer":null,"approvalId":"4ed8867d","calls":[{"key":"call_1","name":"bookFlight","arguments":"{\"flightNumber\":\"VX205\"}"}]}
ledger: []
GET /api/approvals
-> 200 [{"id":"4ed8867d","conversationId":"c1","requestedBy":"carol","calls":[{"key":"call_1","name":"bookFlight","arguments":"{\"flightNumber\":\"VX205\"}"}]}]
POST /api/approvals/4ed8867d/decisions X-Approver: mallory (allow call_1)
-> 403 {"error":"'mallory' may not approve"}
ledger: []
POST /api/approvals/4ed8867d/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/4ed8867d/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/4ed8867d/decisions X-Approver: bob (the same pause again)
-> 409 {"error":"approval 4ed8867d 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=4ed8867d actor=carol tool=bookFlight {"flightNumber":"VX205"}
APPROVER_REFUSED approval=4ed8867d actor=mallory tool=- not in the approver list
DECISION_REJECTED approval=4ed8867d actor=alice tool=- Every tool call of the reply needs a decision; missing: [call_1]
APPROVED approval=4ed8867d actor=alice tool=bookFlight {"flightNumber":"VX205"}
REPLAY_REFUSED approval=4ed8867d 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:6754106b
event:approval-resolved
data:6754106b
GET /ui/approvals (the <pre> element):
<pre>{&quot;flightNumber&quot;:&quot;&lt;img src=x onerror=alert(1)&gt;&quot;}</pre>
POST /ui/approvals/6754106b (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)