Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5x1ufsjyvu9RJFUUb
5.9 KiB
tool-confirmation
Human-in-the-loop tool calls in Spring AI 2.1: requiresConfirmation, ToolCallConfirmationPolicy,
ToolCallDecisions, the pause and the resume, and everything the framework leaves to the
application -- where the paused reply lives, who may approve, a pause that resolves once, an audit
log, and an approver page driven by Server-Sent Events and htmx. The model is a hand-written
ScriptedChatModel, not a language model: every test queues the tool calls the "model" makes, so
what is asserted is Spring AI's real pause/resume code, not how often a real model asks for a tool.
Companion code for Human-in-the-Loop Tool Calls in Spring AI 2.1 on ankurm.com.
Versions
| Component | Version |
|---|---|
| Spring Boot | 4.1.3 |
| Spring AI | 2.1.0 (GA; Maven Central lists it under 2026-11-19) |
| Java | 25 (Temurin 25.0.5.1) |
| htmx / SSE extension | 2.0.11 (vendored in src/main/resources/static/vendor) |
Quickstart
export JAVA_HOME=/path/to/jdk-25
./scripts/run-all.sh
No API key, no network once Maven has its dependencies, no Docker. Every file in output/ is
written by the test suite itself (the Transcript helper), so each number in the article is an
assertion that fails the build when it stops being true.
What is here
| File | What it does |
|---|---|
tools/FlightTools.java |
One harmless tool and two with @Tool(requiresConfirmation = true). |
tools/TransferTools.java |
A tool with no annotation, paused from the outside (by name, or by its arguments). |
tools/DeleteFileTool.java |
The programmatic way: ToolMetadata.builder().requiresConfirmation(true). |
config/ConfirmationPolicies.java |
A ToolCallConfirmationPolicy bean that pauses transfers over USD 1,000. |
config/ChatClientFactory.java |
The one ChatClient construction, shared by the app and the tests. |
approval/ApprovalService.java |
Pause handling, the approver check, resume, audit. |
approval/ApprovalStore.java |
Server-side pauses; take is atomic so one approver wins. |
approval/AuditLog.java |
PAUSED / APPROVED / DENIED / refusals, with who and what. |
approval/ApprovalEvents.java |
SSE fan-out, completed on stop so graceful shutdown is not held up. |
web/ApiController.java |
/api/chat, /api/approvals, /api/approvals/{id}/decisions, /approvals/stream. |
web/ApproverUiController.java |
The htmx fragments, with everything the model chose HTML-escaped. |
static/approver.html |
The approver page: one SSE connection, no polling. |
Output files
| File | What it captures |
|---|---|
output/01-pause-and-resume.txt |
One gated call: pause, nothing runs, allow, run, second model call. |
output/02-whole-reply-pauses.txt |
A harmless call beside a gated one also waits; awaitingDecision lists only the gated key but every call needs a decision. |
output/03-deny-messages.txt |
What the model receives for a bare deny and for a deny with a message. |
output/04-policies.txt |
Ten policy / call combinations: which pause, which run. |
output/05-resume-errors.txt |
The three IllegalArgumentExceptions of a refused resume, and an empty ledger. |
output/06-replay-is-not-prevented.txt |
The same decisions twice: the tool runs twice. |
output/07-multi-round-turn.txt |
A search round before the gated call is missing from the resumed request. |
output/08-tools-property.txt |
spring.ai.tools.confirmation.tools pausing an unannotated tool. |
output/09-confirmation-disabled.txt |
enabled=false: the startup warning, the flag ignored, the policy bean still applies. |
output/10-http-flow.txt |
The API end to end: 200, 403, 400, 200, 409, 404. |
output/11-audit-log.txt |
The audit lines for that exchange. |
output/12-concurrent-approvers.txt |
Sixteen simultaneous approvals: one booking. |
output/13-approver-ui-and-sse.txt |
SSE events, and an injected tool argument rendered escaped. |
output/14-sse-and-graceful-shutdown.txt |
An open SSE stream against Boot's graceful shutdown, with and without closing it first. |
output/15-api-surface.txt |
javap -public of the confirmation classes in the 2.1.0 jars, and the two properties with their defaults (scripts/api-surface.sh). |
output/16-position-keys.txt |
Keys when a provider sends no id or the same id twice: #0, #1. |
output/17-custom-manager.txt |
A ToolCallingManager wrapper that does not delegate the decision overload: the pause works, the resume throws. |
What was NOT run
- No real model. Whether a particular provider's model asks for the tools you expect is a separate question.
- The htmx page was never opened in a browser; the tests check the server side of it (events, fragments, escaping).
- Streaming (
.stream()), session memory and chat-memory resumption are described in the article from the reference documentation only. - The pause store is in memory and does not survive a restart; persisting it is left to you.
- The approver identity is a request header so the check is visible. It is a stand-in for an authenticated principal.