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:
+78
@@ -0,0 +1,78 @@
|
||||
package com.ankurm.toolconfirmation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.ankurm.toolconfirmation.approval.ApprovalException;
|
||||
import com.ankurm.toolconfirmation.approval.ApprovalService;
|
||||
import com.ankurm.toolconfirmation.approval.ChatResult;
|
||||
import com.ankurm.toolconfirmation.support.ScriptedChatModel;
|
||||
import com.ankurm.toolconfirmation.support.TestModelConfig;
|
||||
import com.ankurm.toolconfirmation.support.Transcript;
|
||||
import com.ankurm.toolconfirmation.tools.BookingLedger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** Sixteen approvers click Allow at the same instant: exactly one booking. */
|
||||
@SpringBootTest(properties = "spring.ai.model.chat=none")
|
||||
@Import(TestModelConfig.class)
|
||||
class ConcurrentApprovalTest {
|
||||
|
||||
@Autowired ScriptedChatModel model;
|
||||
@Autowired ApprovalService service;
|
||||
@Autowired BookingLedger ledger;
|
||||
|
||||
@Test
|
||||
void exactlyOneApproverWins() throws Exception {
|
||||
model.reset();
|
||||
ledger.clear();
|
||||
model.callTools(ScriptedChatModel.call("call_1", "bookFlight", "{\"flightNumber\":\"VX205\"}"))
|
||||
.respond("Booked.");
|
||||
ChatResult paused = service.chat("race", "carol", "Book VX205");
|
||||
|
||||
int approvers = 16;
|
||||
CountDownLatch go = new CountDownLatch(1);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(approvers);
|
||||
List<Future<String>> results = new ArrayList<>();
|
||||
for (int i = 0; i < approvers; i++) {
|
||||
results.add(pool.submit(() -> {
|
||||
go.await();
|
||||
try {
|
||||
service.decide(paused.approvalId(), "alice",
|
||||
List.of(new ApprovalService.Verdict("call_1", true, null)));
|
||||
return "200 DONE";
|
||||
}
|
||||
catch (ApprovalException e) {
|
||||
return e.status().value() + " " + e.getMessage().replace(paused.approvalId(), "<id>");
|
||||
}
|
||||
}));
|
||||
}
|
||||
go.countDown();
|
||||
List<String> outcomes = new ArrayList<>();
|
||||
for (Future<String> f : results) {
|
||||
outcomes.add(f.get(30, TimeUnit.SECONDS));
|
||||
}
|
||||
pool.shutdown();
|
||||
pool.awaitTermination(10, TimeUnit.SECONDS);
|
||||
|
||||
try (Transcript t = new Transcript("12-concurrent-approvers.txt",
|
||||
"16 threads approve the same pause at once")) {
|
||||
outcomes.stream().distinct().sorted().forEach(o -> t.line("%2d x %s",
|
||||
outcomes.stream().filter(o::equals).count(), o));
|
||||
t.line("ledger: %s", ledger.entries());
|
||||
}
|
||||
|
||||
assertThat(ledger.entries()).containsExactly("bookFlight VX205");
|
||||
assertThat(outcomes.stream().filter("200 DONE"::equals).count()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user