|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.agentclientprotocol.sdk.agent.support; |
| 6 | + |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.CountDownLatch; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import com.agentclientprotocol.sdk.agent.PromptContext; |
| 14 | +import com.agentclientprotocol.sdk.agent.SyncPromptContext; |
| 15 | +import com.agentclientprotocol.sdk.annotation.AcpAgent; |
| 16 | +import com.agentclientprotocol.sdk.annotation.Cancel; |
| 17 | +import com.agentclientprotocol.sdk.annotation.NewSession; |
| 18 | +import com.agentclientprotocol.sdk.annotation.Prompt; |
| 19 | +import com.agentclientprotocol.sdk.client.AcpAsyncClient; |
| 20 | +import com.agentclientprotocol.sdk.client.AcpClient; |
| 21 | +import com.agentclientprotocol.sdk.spec.AcpSchema; |
| 22 | +import com.agentclientprotocol.sdk.spec.AcpSchema.PromptRequest; |
| 23 | +import com.agentclientprotocol.sdk.spec.AcpSchema.PromptResponse; |
| 24 | +import com.agentclientprotocol.sdk.test.InMemoryTransportPair; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import reactor.core.Disposable; |
| 27 | +import reactor.core.publisher.Mono; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * An annotated {@code @Prompt} method learns of its prompt's cancellation from its context, |
| 33 | + * with no {@code @Cancel} handler or shared state: {@code SyncPromptContext.isCancelled()} or |
| 34 | + * {@code PromptContext.whenCancelled()}. The grace period is long, so a prompt answered |
| 35 | + * promptly with the handler's {@code _meta} was answered by the handler. |
| 36 | + */ |
| 37 | +class AnnotatedPromptCancellationTest { |
| 38 | + |
| 39 | + private static final Duration TIMEOUT = Duration.ofSeconds(5); |
| 40 | + |
| 41 | + private static final String SESSION = "annotated-cancel"; |
| 42 | + |
| 43 | + private static final Map<String, Object> HANDLERS_OWN = Map.of("answeredBy", "handler"); |
| 44 | + |
| 45 | + @AcpAgent |
| 46 | + static class PollingAgent { |
| 47 | + |
| 48 | + final CountDownLatch running = new CountDownLatch(1); |
| 49 | + |
| 50 | + final CountDownLatch sawCancel = new CountDownLatch(1); |
| 51 | + |
| 52 | + final CountDownLatch cancelHandlerRan = new CountDownLatch(1); |
| 53 | + |
| 54 | + @NewSession |
| 55 | + AcpSchema.NewSessionResponse newSession() { |
| 56 | + return new AcpSchema.NewSessionResponse(SESSION, null, null); |
| 57 | + } |
| 58 | + |
| 59 | + @Prompt |
| 60 | + PromptResponse prompt(PromptRequest request, SyncPromptContext context) { |
| 61 | + running.countDown(); |
| 62 | + while (!context.isCancelled()) { |
| 63 | + Thread.interrupted(); // a cancelled request interrupts the thread: keep polling |
| 64 | + pause(); |
| 65 | + } |
| 66 | + sawCancel.countDown(); |
| 67 | + return new PromptResponse(AcpSchema.StopReason.CANCELLED, HANDLERS_OWN); |
| 68 | + } |
| 69 | + |
| 70 | + @Cancel |
| 71 | + void cancel() { |
| 72 | + cancelHandlerRan.countDown(); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @AcpAgent |
| 78 | + static class ReactiveAgent { |
| 79 | + |
| 80 | + @NewSession |
| 81 | + AcpSchema.NewSessionResponse newSession() { |
| 82 | + return new AcpSchema.NewSessionResponse(SESSION, null, null); |
| 83 | + } |
| 84 | + |
| 85 | + @Prompt |
| 86 | + Mono<PromptResponse> prompt(PromptContext context) { |
| 87 | + return Mono.<PromptResponse>never() |
| 88 | + .takeUntilOther(context.whenCancelled()) |
| 89 | + .switchIfEmpty(Mono.fromSupplier( |
| 90 | + () -> new PromptResponse(AcpSchema.StopReason.CANCELLED, HANDLERS_OWN))); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void aPollingPromptAnswersCancelledPromptlyOnSessionCancel() throws Exception { |
| 97 | + PollingAgent bean = new PollingAgent(); |
| 98 | + run(bean, client -> { |
| 99 | + Mono<PromptResponse> response = client.prompt(prompt()).cache(); |
| 100 | + response.subscribe(r -> { |
| 101 | + }, e -> { |
| 102 | + }); |
| 103 | + await(bean.running); |
| 104 | + long start = System.nanoTime(); |
| 105 | + client.cancel(new AcpSchema.CancelNotification(SESSION)).block(TIMEOUT); |
| 106 | + |
| 107 | + PromptResponse answer = response.block(TIMEOUT); |
| 108 | + assertThat(answer.stopReason()).isEqualTo(AcpSchema.StopReason.CANCELLED); |
| 109 | + assertThat(answer.meta()).as("the handler's own answer").isEqualTo(HANDLERS_OWN); |
| 110 | + assertThat(Duration.ofNanos(System.nanoTime() - start)).isLessThan(Duration.ofSeconds(3)); |
| 111 | + await(bean.cancelHandlerRan); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void aPollingPromptSeesACancelRequestForItsRequest() throws Exception { |
| 117 | + PollingAgent bean = new PollingAgent(); |
| 118 | + run(bean, client -> { |
| 119 | + Disposable prompt = client.prompt(prompt()).subscribe(r -> { |
| 120 | + }, e -> { |
| 121 | + }); |
| 122 | + await(bean.running); |
| 123 | + |
| 124 | + prompt.dispose(); // $/cancel_request |
| 125 | + |
| 126 | + await(bean.sawCancel); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void aReactivePromptCompletesOnWhenCancelled() throws Exception { |
| 132 | + run(new ReactiveAgent(), client -> { |
| 133 | + Mono<PromptResponse> response = client.prompt(prompt()).cache(); |
| 134 | + response.subscribe(r -> { |
| 135 | + }, e -> { |
| 136 | + }); |
| 137 | + pause(); |
| 138 | + client.cancel(new AcpSchema.CancelNotification(SESSION)).block(TIMEOUT); |
| 139 | + |
| 140 | + assertThat(response.block(TIMEOUT).meta()).isEqualTo(HANDLERS_OWN); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + interface ClientScenario { |
| 145 | + |
| 146 | + void run(AcpAsyncClient client) throws Exception; |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + private static void run(Object bean, ClientScenario scenario) throws Exception { |
| 151 | + InMemoryTransportPair pair = InMemoryTransportPair.create(); |
| 152 | + AcpAgentSupport agent = AcpAgentSupport.create(bean) |
| 153 | + .transport(pair.agentTransport()) |
| 154 | + .requestTimeout(TIMEOUT) |
| 155 | + .cancelGracePeriod(Duration.ofSeconds(30)) |
| 156 | + .build(); |
| 157 | + agent.start(); |
| 158 | + AcpAsyncClient client = AcpClient.async(pair.clientTransport()).requestTimeout(TIMEOUT).build(); |
| 159 | + try { |
| 160 | + client.initialize().block(TIMEOUT); |
| 161 | + client.newSession(new AcpSchema.NewSessionRequest("/w", List.of())).block(TIMEOUT); |
| 162 | + scenario.run(client); |
| 163 | + } |
| 164 | + finally { |
| 165 | + client.closeGracefully().block(TIMEOUT); |
| 166 | + agent.close(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static PromptRequest prompt() { |
| 171 | + return new PromptRequest(SESSION, List.of(new AcpSchema.TextContent("work"))); |
| 172 | + } |
| 173 | + |
| 174 | + private static void await(CountDownLatch latch) throws InterruptedException { |
| 175 | + assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); |
| 176 | + } |
| 177 | + |
| 178 | + private static void pause() { |
| 179 | + try { |
| 180 | + Thread.sleep(20); |
| 181 | + } |
| 182 | + catch (InterruptedException ex) { |
| 183 | + Thread.currentThread().interrupt(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments