|
12 | 12 | import java.util.concurrent.TimeUnit; |
13 | 13 | import java.util.concurrent.atomic.AtomicReference; |
14 | 14 |
|
| 15 | +import com.agentclientprotocol.sdk.CapturedLogs; |
15 | 16 | import com.agentclientprotocol.sdk.client.AcpAsyncClient; |
16 | 17 | import com.agentclientprotocol.sdk.client.AcpClient; |
17 | 18 | import com.agentclientprotocol.sdk.error.AcpProtocolException; |
@@ -110,6 +111,66 @@ void executeCancelledWhileWaitingForExitReleasesTheTerminal_terminalsMdxMustRele |
110 | 111 | agent.closeGracefully().block(TIMEOUT); |
111 | 112 | } |
112 | 113 |
|
| 114 | + /** |
| 115 | + * A release that the client answers with an error is logged at WARN by its code and the |
| 116 | + * SDK's description of the code: the client's message and data are its own text and stay |
| 117 | + * at DEBUG. |
| 118 | + */ |
| 119 | + @Test |
| 120 | + void aFailedReleaseOfACancelledCommandIsLoggedWithoutTheClientsMessageOrData() throws Exception { |
| 121 | + CountDownLatch created = new CountDownLatch(1); |
| 122 | + |
| 123 | + AcpAsyncAgent agent = AcpAgent.async(this.transportPair.agentTransport()) |
| 124 | + .requestTimeout(TIMEOUT) |
| 125 | + .initializeHandler(req -> Mono.just(InitializeResponse.ok())) |
| 126 | + .newSessionHandler(req -> Mono.just(new NewSessionResponse("s1", null, null))) |
| 127 | + .promptHandler((request, context) -> context.execute("sleep", "600").thenReturn(PromptResponse.endTurn())) |
| 128 | + .build(); |
| 129 | + |
| 130 | + AcpAsyncClient client = AcpClient.async(this.transportPair.clientTransport()) |
| 131 | + .requestTimeout(TIMEOUT) |
| 132 | + .clientCapabilities(new ClientCapabilities(null, true)) |
| 133 | + .createTerminalHandler(req -> { |
| 134 | + created.countDown(); |
| 135 | + return Mono.just(new CreateTerminalResponse("term-1")); |
| 136 | + }) |
| 137 | + .waitForTerminalExitHandler(req -> Mono.never()) |
| 138 | + .terminalOutputHandler(req -> Mono.never()) |
| 139 | + .releaseTerminalHandler(req -> Mono.error(new AcpProtocolException(AcpErrorCodes.RESOURCE_NOT_FOUND, |
| 140 | + "MESSAGE-SECRET", Map.of("details", "DATA-SECRET")))) |
| 141 | + .killTerminalHandler(req -> Mono.just(new AcpSchema.KillTerminalCommandResponse())) |
| 142 | + .build(); |
| 143 | + |
| 144 | + try (CapturedLogs logs = CapturedLogs.open()) { |
| 145 | + agent.start().block(TIMEOUT); |
| 146 | + connect(client); |
| 147 | + |
| 148 | + Disposable turn = client.prompt(prompt()).subscribe(r -> { |
| 149 | + }, e -> { |
| 150 | + }); |
| 151 | + assertThat(created.await(5, TimeUnit.SECONDS)).as("terminal/create").isTrue(); |
| 152 | + Thread.sleep(200); |
| 153 | + turn.dispose(); |
| 154 | + |
| 155 | + long deadline = System.nanoTime() + TIMEOUT.toNanos(); |
| 156 | + while (logs.events() |
| 157 | + .stream() |
| 158 | + .noneMatch(e -> e.getFormattedMessage().contains("of a cancelled command")) |
| 159 | + && System.nanoTime() < deadline) { |
| 160 | + Thread.sleep(20); |
| 161 | + } |
| 162 | + assertThat(logs.events()).filteredOn(e -> e.getLevel() == ch.qos.logback.classic.Level.WARN) |
| 163 | + .filteredOn(e -> e.getFormattedMessage().contains("Could not release terminal")) |
| 164 | + .singleElement() |
| 165 | + .satisfies(e -> assertThat(e.getFormattedMessage()).contains("-32002").contains("Resource not found")); |
| 166 | + logs.assertNoneAtInfoOrAboveContains("MESSAGE-SECRET"); |
| 167 | + logs.assertNoneAtInfoOrAboveContains("DATA-SECRET"); |
| 168 | + } |
| 169 | + |
| 170 | + client.closeGracefully().block(TIMEOUT); |
| 171 | + agent.closeGracefully().block(TIMEOUT); |
| 172 | + } |
| 173 | + |
113 | 174 | /** The same for a sync handler: cancelling the prompt interrupts it in {@code execute}. */ |
114 | 175 | @Test |
115 | 176 | void syncExecuteCancelledWhileWaitingForExitReleasesTheTerminal_terminalsMdxMustRelease() throws Exception { |
|
0 commit comments