|
| 1 | +package com.agentclientprotocol.sdk.integration; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 6 | + |
| 7 | +import com.agentclientprotocol.sdk.agent.AcpAgent; |
| 8 | +import com.agentclientprotocol.sdk.agent.AcpSyncAgent; |
| 9 | +import com.agentclientprotocol.sdk.client.AcpClient; |
| 10 | +import com.agentclientprotocol.sdk.client.AcpSyncClient; |
| 11 | +import com.agentclientprotocol.sdk.spec.AcpSchema; |
| 12 | +import com.agentclientprotocol.sdk.test.InMemoryTransportPair; |
| 13 | +import org.junit.jupiter.api.RepeatedTest; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +/** |
| 18 | + * Reproduction for issue #14: AcpSyncAgent + AcpSyncClient over InMemoryTransportPair, one |
| 19 | + * session, three sequential prompts, run under {@code taskset -c 0}. The report says the |
| 20 | + * second or third prompt never returns and the request timeout fires. |
| 21 | + */ |
| 22 | +class MultiPromptSyncSessionTest { |
| 23 | + |
| 24 | + private static final Duration TIMEOUT = Duration.ofSeconds(5); |
| 25 | + |
| 26 | + @RepeatedTest(5) |
| 27 | + void threeSequentialPromptsOnOneSessionAllReturn() { |
| 28 | + InMemoryTransportPair pair = InMemoryTransportPair.create(); |
| 29 | + AcpSyncAgent agent = AcpAgent.sync(pair.agentTransport()) |
| 30 | + .requestTimeout(TIMEOUT) |
| 31 | + .initializeHandler(request -> new AcpSchema.InitializeResponse(1, new AcpSchema.AgentCapabilities(), List.of())) |
| 32 | + .newSessionHandler(request -> new AcpSchema.NewSessionResponse("session-14", null, null)) |
| 33 | + .promptHandler((request, context) -> new AcpSchema.PromptResponse(AcpSchema.StopReason.END_TURN)) |
| 34 | + .build(); |
| 35 | + agent.start(); |
| 36 | + |
| 37 | + AcpSyncClient client = AcpClient.sync(pair.clientTransport()).requestTimeout(TIMEOUT).build(); |
| 38 | + try { |
| 39 | + client.initialize(); |
| 40 | + String sessionId = client.newSession(new AcpSchema.NewSessionRequest("/workspace", List.of())).sessionId(); |
| 41 | + for (int i = 1; i <= 3; i++) { |
| 42 | + long t0 = System.nanoTime(); |
| 43 | + AcpSchema.PromptResponse response = client.prompt(new AcpSchema.PromptRequest(sessionId, |
| 44 | + List.of(new AcpSchema.TextContent("prompt " + i)))); |
| 45 | + System.out.printf("prompt %d returned %s in %d ms%n", i, response.stopReason(), |
| 46 | + (System.nanoTime() - t0) / 1_000_000); |
| 47 | + assertThat(response.stopReason()).isEqualTo(AcpSchema.StopReason.END_TURN); |
| 48 | + } |
| 49 | + } |
| 50 | + finally { |
| 51 | + client.closeGracefully(); |
| 52 | + agent.closeGracefully(); |
| 53 | + pair.closeGracefully().block(TIMEOUT); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @RepeatedTest(5) |
| 58 | + void threeSequentialStreamingPromptsOnOneSessionAllReturn() { |
| 59 | + InMemoryTransportPair pair = InMemoryTransportPair.create(); |
| 60 | + AcpSyncAgent agent = AcpAgent.sync(pair.agentTransport()) |
| 61 | + .requestTimeout(TIMEOUT) |
| 62 | + .initializeHandler(request -> new AcpSchema.InitializeResponse(1, new AcpSchema.AgentCapabilities(), List.of())) |
| 63 | + .newSessionHandler(request -> new AcpSchema.NewSessionResponse("session-14", null, null)) |
| 64 | + .promptHandler((request, context) -> { |
| 65 | + for (int chunk = 0; chunk < 3; chunk++) { |
| 66 | + context.sendUpdate(request.sessionId(), |
| 67 | + new AcpSchema.AgentMessageChunk("agent_message_chunk", new AcpSchema.TextContent("chunk " + chunk))); |
| 68 | + } |
| 69 | + return new AcpSchema.PromptResponse(AcpSchema.StopReason.END_TURN); |
| 70 | + }) |
| 71 | + .build(); |
| 72 | + agent.start(); |
| 73 | + |
| 74 | + List<String> updates = new CopyOnWriteArrayList<>(); |
| 75 | + AcpSyncClient client = AcpClient.sync(pair.clientTransport()) |
| 76 | + .requestTimeout(TIMEOUT) |
| 77 | + .sessionUpdateConsumer(notification -> updates.add(notification.update().getClass().getSimpleName())) |
| 78 | + .build(); |
| 79 | + try { |
| 80 | + client.initialize(); |
| 81 | + String sessionId = client.newSession(new AcpSchema.NewSessionRequest("/workspace", List.of())).sessionId(); |
| 82 | + for (int i = 1; i <= 3; i++) { |
| 83 | + long t0 = System.nanoTime(); |
| 84 | + AcpSchema.PromptResponse response = client.prompt(new AcpSchema.PromptRequest(sessionId, |
| 85 | + List.of(new AcpSchema.TextContent("prompt " + i)))); |
| 86 | + System.out.printf("streaming prompt %d returned %s in %d ms (%d updates so far)%n", i, |
| 87 | + response.stopReason(), (System.nanoTime() - t0) / 1_000_000, updates.size()); |
| 88 | + assertThat(response.stopReason()).isEqualTo(AcpSchema.StopReason.END_TURN); |
| 89 | + } |
| 90 | + } |
| 91 | + finally { |
| 92 | + client.closeGracefully(); |
| 93 | + agent.closeGracefully(); |
| 94 | + pair.closeGracefully().block(TIMEOUT); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments