From 7b80d72d182e1e95fdbf0d09a2c0ecf30e032324 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 5 May 2026 00:54:15 +0000 Subject: [PATCH 1/3] Initial plan From 0b6c28ba7fe42924e7febdb69b539296005c6aed Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 5 May 2026 01:19:55 +0000 Subject: [PATCH 2/3] Port reference implementation changes: process cleanup, connect fallback, and E2E test coverage Source changes from c063458 (Expand SDK E2E runtime coverage): - Add waitFor() after destroyForcibly() in process cleanup - Expand connect method fallback to match 'Unhandled method connect' message - Extract formatCliExitedMessage helper for consistent error formatting - Wait for stderr reader before throwing in port announcement New E2E tests ported: - EventFidelityTest: assistant.usage and session.usage_info event tests - ToolResultsTest: rejected and denied resultType handling - StreamingFidelityTest: streaming disabled on resume, reasoning effort - ToolsTest: parallel tools, availableTools/excludedTools combined - PermissionsTest: noResult kind, setApproveAll, slow handler Co-authored-by: edburns <75821+edburns@users.noreply.github.com> --- .../github/copilot/sdk/CliServerManager.java | 26 ++- .../com/github/copilot/sdk/CopilotClient.java | 18 ++- .../github/copilot/sdk/EventFidelityTest.java | 111 +++++++++++++ .../github/copilot/sdk/PermissionsTest.java | 110 +++++++++++++ .../copilot/sdk/StreamingFidelityTest.java | 93 +++++++++++ .../github/copilot/sdk/ToolResultsTest.java | 148 ++++++++++++++++++ .../com/github/copilot/sdk/ToolsTest.java | 105 +++++++++++++ 7 files changed, 601 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/github/copilot/sdk/EventFidelityTest.java create mode 100644 src/test/java/com/github/copilot/sdk/ToolResultsTest.java diff --git a/src/main/java/com/github/copilot/sdk/CliServerManager.java b/src/main/java/com/github/copilot/sdk/CliServerManager.java index b6009e839..758ecfca2 100644 --- a/src/main/java/com/github/copilot/sdk/CliServerManager.java +++ b/src/main/java/com/github/copilot/sdk/CliServerManager.java @@ -33,6 +33,7 @@ final class CliServerManager { private final CopilotClientOptions options; private final StringBuilder stderrBuffer = new StringBuilder(); + private volatile Thread stderrThread; private String connectionToken; CliServerManager(CopilotClientOptions options) { @@ -199,7 +200,7 @@ JsonRpcClient connectToServer(Process process, String tcpHost, Integer tcpPort) } private void startStderrReader(Process process) { - var stderrThread = new Thread(() -> { + var thread = new Thread(() -> { try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { String line; @@ -213,8 +214,9 @@ private void startStderrReader(Process process) { LOG.log(Level.FINE, "Error reading stderr", e); } }, "cli-stderr-reader"); - stderrThread.setDaemon(true); - stderrThread.start(); + thread.setDaemon(true); + thread.start(); + this.stderrThread = thread; } private Integer waitForPortAnnouncement(Process process) throws IOException { @@ -226,11 +228,10 @@ private Integer waitForPortAnnouncement(Process process) throws IOException { while (System.currentTimeMillis() < deadline) { String line = reader.readLine(); if (line == null) { + awaitStderrReader(); String stderr = getStderrOutput(); - if (!stderr.isEmpty()) { - throw new IOException("CLI process exited unexpectedly. stderr: " + stderr); - } - throw new IOException("CLI process exited unexpectedly"); + throw new IOException( + CopilotClient.formatCliExitedMessage("CLI process exited unexpectedly.", stderr)); } Matcher matcher = portPattern.matcher(line); @@ -250,6 +251,17 @@ String getStderrOutput() { } } + private void awaitStderrReader() { + Thread t = this.stderrThread; + if (t != null) { + try { + t.join(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + private void clearStderrBuffer() { synchronized (stderrBuffer) { stderrBuffer.setLength(0); diff --git a/src/main/java/com/github/copilot/sdk/CopilotClient.java b/src/main/java/com/github/copilot/sdk/CopilotClient.java index 555e88861..bad4c13c1 100644 --- a/src/main/java/com/github/copilot/sdk/CopilotClient.java +++ b/src/main/java/com/github/copilot/sdk/CopilotClient.java @@ -216,12 +216,20 @@ private Connection startCoreBody() { } catch (Exception e) { String stderr = serverManager.getStderrOutput(); if (!stderr.isEmpty()) { - throw new CompletionException(new IOException("CLI process exited unexpectedly. stderr: " + stderr, e)); + throw new CompletionException( + new IOException(formatCliExitedMessage("CLI process exited unexpectedly.", stderr), e)); } throw new CompletionException(e); } } + static String formatCliExitedMessage(String message, String stderrOutput) { + if (stderrOutput == null || stderrOutput.isEmpty()) { + return message; + } + return message + "\nstderr: " + stderrOutput; + } + private static final int MIN_PROTOCOL_VERSION = 2; private static final int METHOD_NOT_FOUND_ERROR_CODE = -32601; @@ -244,7 +252,7 @@ private void verifyProtocolVersion(Connection connection) throws Exception { while (cause instanceof java.util.concurrent.ExecutionException || cause instanceof CompletionException) { cause = cause.getCause(); } - if (cause instanceof JsonRpcException rpcEx && rpcEx.getCode() == METHOD_NOT_FOUND_ERROR_CODE) { + if (cause instanceof JsonRpcException rpcEx && isUnsupportedConnectMethod(rpcEx)) { // Legacy server without 'connect'; fall back to 'ping'. // A token, if any, is silently dropped — the legacy server can't enforce one. var params = new HashMap(); @@ -270,6 +278,10 @@ private void verifyProtocolVersion(Connection connection) throws Exception { } } + private static boolean isUnsupportedConnectMethod(JsonRpcException ex) { + return ex.getCode() == METHOD_NOT_FOUND_ERROR_CODE || "Unhandled method connect".equals(ex.getMessage()); + } + /** * Disconnects from the Copilot server and closes all active sessions. *

@@ -348,7 +360,7 @@ private CompletableFuture cleanupConnection() { if (connection.process != null) { try { if (connection.process.isAlive()) { - connection.process.destroyForcibly(); + connection.process.destroyForcibly().waitFor(); } } catch (Exception e) { LOG.log(Level.FINE, "Error killing process", e); diff --git a/src/test/java/com/github/copilot/sdk/EventFidelityTest.java b/src/test/java/com/github/copilot/sdk/EventFidelityTest.java new file mode 100644 index 000000000..60b8e1327 --- /dev/null +++ b/src/test/java/com/github/copilot/sdk/EventFidelityTest.java @@ -0,0 +1,111 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------------------------------------------*/ + +package com.github.copilot.sdk; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.github.copilot.sdk.generated.AssistantUsageEvent; +import com.github.copilot.sdk.generated.SessionEvent; +import com.github.copilot.sdk.generated.SessionUsageInfoEvent; +import com.github.copilot.sdk.json.MessageOptions; +import com.github.copilot.sdk.json.PermissionHandler; +import com.github.copilot.sdk.json.SessionConfig; + +/** + * E2E tests for event fidelity — verifying the shape, ordering, and presence of + * key events emitted from the runtime. + * + *

+ * Snapshots are stored in {@code test/snapshots/event_fidelity/}. + *

+ */ +public class EventFidelityTest { + + private static E2ETestContext ctx; + + @BeforeAll + static void setup() throws Exception { + ctx = E2ETestContext.create(); + } + + @AfterAll + static void teardown() throws Exception { + if (ctx != null) { + ctx.close(); + } + } + + /** + * Verifies that an {@code assistant.usage} event is emitted after the model + * processes a prompt. + * + * @see Snapshot: + * event_fidelity/should_emit_assistant_usage_event_after_model_call + */ + @Test + void testShouldEmitAssistantUsageEventAfterModelCall() throws Exception { + ctx.configureForTest("event_fidelity", "should_emit_assistant_usage_event_after_model_call"); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client + .createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get(); + + List events = new ArrayList<>(); + session.on(events::add); + + session.sendAndWait(new MessageOptions().setPrompt("What is 5+5? Reply with just the number.")).get(60, + TimeUnit.SECONDS); + + List usageEvents = events.stream().filter(e -> e instanceof AssistantUsageEvent) + .map(e -> (AssistantUsageEvent) e).toList(); + + assertFalse(usageEvents.isEmpty(), "Should have received an assistant.usage event after model call"); + + AssistantUsageEvent lastUsage = usageEvents.get(usageEvents.size() - 1); + assertNotNull(lastUsage.getData().model(), "Usage event should have a model field"); + assertFalse(lastUsage.getData().model().isEmpty(), "Model field should not be empty"); + + session.close(); + } + } + + /** + * Verifies that a {@code session.usage_info} event is emitted after the model + * processes a prompt. + * + * @see Snapshot: + * event_fidelity/should_emit_session_usage_info_event_after_model_call + */ + @Test + void testShouldEmitSessionUsageInfoEventAfterModelCall() throws Exception { + ctx.configureForTest("event_fidelity", "should_emit_session_usage_info_event_after_model_call"); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client + .createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get(); + + List events = new ArrayList<>(); + session.on(events::add); + + session.sendAndWait(new MessageOptions().setPrompt("What is 5+5? Reply with just the number.")).get(60, + TimeUnit.SECONDS); + + List usageInfoEvents = events.stream() + .filter(e -> e instanceof SessionUsageInfoEvent).map(e -> (SessionUsageInfoEvent) e).toList(); + + assertFalse(usageInfoEvents.isEmpty(), "Should have received a session.usage_info event after model call"); + + session.close(); + } + } +} diff --git a/src/test/java/com/github/copilot/sdk/PermissionsTest.java b/src/test/java/com/github/copilot/sdk/PermissionsTest.java index b498c1ce7..9cd7ca223 100644 --- a/src/test/java/com/github/copilot/sdk/PermissionsTest.java +++ b/src/test/java/com/github/copilot/sdk/PermissionsTest.java @@ -363,4 +363,114 @@ void testShouldDenyToolOperationsWhenHandlerExplicitlyDeniesAfterResume(TestInfo session2.close(); } } + + /** + * Verifies that a permission handler returning {@code noResult} is handled + * correctly — the handler is called, and the session can be aborted afterward. + * + * @see Snapshot: permissions/should_deny_permission_with_noresult_kind + */ + @Test + void testShouldDenyPermissionWithNoResultKind() throws Exception { + ctx.configureForTest("permissions", "should_deny_permission_with_noresult_kind"); + + var permissionCalled = new CompletableFuture(); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client + .createSession(new SessionConfig().setOnPermissionRequest((request, invocation) -> { + permissionCalled.complete(true); + return CompletableFuture.completedFuture( + new PermissionRequestResult().setKind(PermissionRequestResultKind.NO_RESULT)); + })).get(); + + session.send(new MessageOptions().setPrompt("Run 'node --version'")); + + assertTrue(permissionCalled.get(30, TimeUnit.SECONDS), + "Expected the no-result permission handler to be called."); + + session.abort().get(10, TimeUnit.SECONDS); + session.close(); + } + } + + /** + * Verifies that the runtime short-circuits the permission handler when + * {@code session.permissions.setApproveAll(true)} has been called. + * + * @see Snapshot: + * permissions/should_short_circuit_permission_handler_when_set_approve_all_enabled + */ + @Test + void testShouldShortCircuitPermissionHandlerWhenSetApproveAllEnabled() throws Exception { + ctx.configureForTest("permissions", "should_short_circuit_permission_handler_when_set_approve_all_enabled"); + + var handlerCallCount = new int[]{0}; + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client + .createSession(new SessionConfig().setOnPermissionRequest((request, invocation) -> { + handlerCallCount[0]++; + return CompletableFuture.completedFuture( + new PermissionRequestResult().setKind(PermissionRequestResultKind.APPROVED)); + })).get(); + + // Set approve-all so the runtime short-circuits + var setResult = session.getRpc().permissions + .setApproveAll(new com.github.copilot.sdk.generated.rpc.SessionPermissionsSetApproveAllParams( + session.getSessionId(), true)) + .get(10, TimeUnit.SECONDS); + assertTrue(setResult.success(), "setApproveAll should succeed"); + + AssistantMessageEvent response = session + .sendAndWait(new MessageOptions().setPrompt("Run 'echo test' and tell me what happens")) + .get(60, TimeUnit.SECONDS); + assertNotNull(response); + + // Handler should not have been called since runtime approves all + assertEquals(0, handlerCallCount[0], + "Permission handler should not be called when setApproveAll is enabled"); + + session.close(); + } + } + + /** + * Verifies that the SDK correctly waits for a slow permission handler before + * completing tool execution. + * + * @see Snapshot: permissions/should_wait_for_slow_permission_handler + */ + @Test + void testShouldWaitForSlowPermissionHandler() throws Exception { + ctx.configureForTest("permissions", "should_wait_for_slow_permission_handler"); + + var handlerEntered = new CompletableFuture(); + var releaseHandler = new CompletableFuture(); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client + .createSession(new SessionConfig().setOnPermissionRequest((request, invocation) -> { + handlerEntered.complete(null); + return releaseHandler.thenApply( + v -> new PermissionRequestResult().setKind(PermissionRequestResultKind.APPROVED)); + })).get(); + + // Use send (non-blocking) so we can interact with the handler + CompletableFuture responseFuture = session + .sendAndWait(new MessageOptions().setPrompt("Run 'echo slow_handler_test'")); + + // Wait for permission handler to be entered + handlerEntered.get(30, TimeUnit.SECONDS); + + // Release the handler + releaseHandler.complete(null); + + // Session should complete successfully + AssistantMessageEvent message = responseFuture.get(60, TimeUnit.SECONDS); + assertNotNull(message); + + session.close(); + } + } } diff --git a/src/test/java/com/github/copilot/sdk/StreamingFidelityTest.java b/src/test/java/com/github/copilot/sdk/StreamingFidelityTest.java index f3f597652..e922f4bf0 100644 --- a/src/test/java/com/github/copilot/sdk/StreamingFidelityTest.java +++ b/src/test/java/com/github/copilot/sdk/StreamingFidelityTest.java @@ -182,4 +182,97 @@ void testShouldProduceDeltasAfterSessionResume() throws Exception { } } } + + /** + * Verifies that no delta events are produced after resuming a session with + * streaming disabled (even though it was originally created with streaming + * enabled). + * + * @see Snapshot: + * streaming_fidelity/should_not_produce_deltas_after_session_resume_with_streaming_disabled + */ + @Test + void testShouldNotProduceDeltasAfterSessionResumeWithStreamingDisabled() throws Exception { + ctx.configureForTest("streaming_fidelity", + "should_not_produce_deltas_after_session_resume_with_streaming_disabled"); + + try (CopilotClient client = ctx.createClient()) { + // Create a streaming session and send an initial message + CopilotSession session = client.createSession( + new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setStreaming(true)).get(); + session.sendAndWait(new MessageOptions().setPrompt("What is 3 + 6?")).get(60, TimeUnit.SECONDS); + String sessionId = session.getSessionId(); + session.close(); + + // Resume using a new client with streaming DISABLED + try (CopilotClient newClient = ctx.createClient()) { + CopilotSession session2 = newClient.resumeSession(sessionId, new ResumeSessionConfig() + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setStreaming(false)).get(); + + List events = new ArrayList<>(); + session2.on(events::add); + + AssistantMessageEvent answer = session2 + .sendAndWait(new MessageOptions().setPrompt("Now if you double that, what do you get?")) + .get(60, TimeUnit.SECONDS); + assertNotNull(answer); + assertTrue(answer.getData().content().contains("18"), + "Follow-up response should contain 18: " + answer.getData().content()); + + // No deltas when streaming is toggled off + List deltaEvents = events.stream() + .filter(e -> e instanceof AssistantMessageDeltaEvent).map(e -> (AssistantMessageDeltaEvent) e) + .toList(); + assertTrue(deltaEvents.isEmpty(), + "Should not receive delta events when streaming is disabled on resume"); + + // But should still have a final assistant.message + List assistantEvents = events.stream() + .filter(e -> e instanceof AssistantMessageEvent).map(e -> (AssistantMessageEvent) e).toList(); + assertFalse(assistantEvents.isEmpty(), + "Should still have a final assistant.message when streaming is disabled"); + + session2.close(); + } + } + } + + /** + * Verifies that setting reasoningEffort alongside streaming=true does not break + * the streaming pipeline — deltas still arrive and complete successfully. + * + * @see Snapshot: + * streaming_fidelity/should_emit_streaming_deltas_with_reasoning_effort_configured + */ + @Test + void testShouldEmitStreamingDeltasWithReasoningEffortConfigured() throws Exception { + ctx.configureForTest("streaming_fidelity", "should_emit_streaming_deltas_with_reasoning_effort_configured"); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client + .createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL) + .setStreaming(true).setReasoningEffort("high")) + .get(); + + List events = new ArrayList<>(); + session.on(events::add); + + session.sendAndWait(new MessageOptions().setPrompt("What is 15 * 17?")).get(60, TimeUnit.SECONDS); + + // With streaming + reasoning effort, we should still get content deltas + List deltaEvents = events.stream() + .filter(e -> e instanceof AssistantMessageDeltaEvent).map(e -> (AssistantMessageDeltaEvent) e) + .toList(); + assertFalse(deltaEvents.isEmpty(), "Should have received delta events with reasoning effort configured"); + + // And a final assistant.message with the answer + List assistantEvents = events.stream() + .filter(e -> e instanceof AssistantMessageEvent).map(e -> (AssistantMessageEvent) e).toList(); + assertFalse(assistantEvents.isEmpty(), "Should have received assistant message events"); + assertTrue(assistantEvents.get(assistantEvents.size() - 1).getData().content().contains("255"), + "Response should contain 255"); + + session.close(); + } + } } diff --git a/src/test/java/com/github/copilot/sdk/ToolResultsTest.java b/src/test/java/com/github/copilot/sdk/ToolResultsTest.java new file mode 100644 index 000000000..31f9d1b07 --- /dev/null +++ b/src/test/java/com/github/copilot/sdk/ToolResultsTest.java @@ -0,0 +1,148 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------------------------------------------*/ + +package com.github.copilot.sdk; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.github.copilot.sdk.generated.SessionEvent; +import com.github.copilot.sdk.generated.ToolExecutionCompleteEvent; +import com.github.copilot.sdk.json.MessageOptions; +import com.github.copilot.sdk.json.PermissionHandler; +import com.github.copilot.sdk.json.SessionConfig; +import com.github.copilot.sdk.json.ToolDefinition; +import com.github.copilot.sdk.json.ToolResultObject; + +/** + * E2E tests for tool result types — verifying that rejected and denied result + * types are handled correctly by the runtime. + * + *

+ * Snapshots are stored in {@code test/snapshots/tool_results/}. + *

+ */ +public class ToolResultsTest { + + private static E2ETestContext ctx; + + @BeforeAll + static void setup() throws Exception { + ctx = E2ETestContext.create(); + } + + @AfterAll + static void teardown() throws Exception { + if (ctx != null) { + ctx.close(); + } + } + + /** + * Verifies that a tool returning a "rejected" resultType is reported as a + * failed tool execution with the correct error code. + * + * @see Snapshot: + * tool_results/should_handle_tool_result_with_rejected_resulttype + */ + @Test + void testShouldHandleToolResultWithRejectedResultType() throws Exception { + ctx.configureForTest("tool_results", "should_handle_tool_result_with_rejected_resulttype"); + + var toolHandlerCalled = new boolean[]{false}; + + Map params = Map.of("type", "object", "properties", Map.of(), "required", List.of()); + + ToolDefinition deployTool = ToolDefinition.create("deploy_service", "Deploys a service", params, + (invocation) -> { + toolHandlerCalled[0] = true; + return CompletableFuture.completedFuture(new ToolResultObject("rejected", + "Deployment rejected: policy violation - production deployments require approval", null, + null, null, null)); + }); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client.createSession(new SessionConfig().setTools(List.of(deployTool)) + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get(); + + List events = new ArrayList<>(); + session.on(events::add); + + session.sendAndWait(new MessageOptions().setPrompt( + "Deploy the service using deploy_service. If it's rejected, tell me it was 'rejected by policy'.")) + .get(60, TimeUnit.SECONDS); + + assertTrue(toolHandlerCalled[0], "Tool handler should have been called"); + + List toolEvents = events.stream() + .filter(e -> e instanceof ToolExecutionCompleteEvent).map(e -> (ToolExecutionCompleteEvent) e) + .toList(); + assertFalse(toolEvents.isEmpty(), "Should have a tool.execution_complete event"); + + ToolExecutionCompleteEvent toolEvt = toolEvents.get(0); + assertFalse(toolEvt.getData().success(), "Tool execution should not be marked as successful"); + assertNotNull(toolEvt.getData().error(), "Should have error details"); + assertEquals("rejected", toolEvt.getData().error().code(), "Error code should be 'rejected'"); + + session.close(); + } + } + + /** + * Verifies that a tool returning a "denied" resultType is reported as a failed + * tool execution with the correct error code. + * + * @see Snapshot: tool_results/should_handle_tool_result_with_denied_resulttype + */ + @Test + void testShouldHandleToolResultWithDeniedResultType() throws Exception { + ctx.configureForTest("tool_results", "should_handle_tool_result_with_denied_resulttype"); + + var toolHandlerCalled = new boolean[]{false}; + + Map params = Map.of("type", "object", "properties", Map.of(), "required", List.of()); + + ToolDefinition accessTool = ToolDefinition.create("access_secret", "Accesses a secret", params, + (invocation) -> { + toolHandlerCalled[0] = true; + return CompletableFuture.completedFuture(new ToolResultObject("denied", + "Access denied: insufficient permissions to read secrets", null, null, null, null)); + }); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client.createSession(new SessionConfig().setTools(List.of(accessTool)) + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get(); + + List events = new ArrayList<>(); + session.on(events::add); + + session.sendAndWait(new MessageOptions().setPrompt( + "Use access_secret to get the API key. If access is denied, tell me it was 'access denied'.")) + .get(60, TimeUnit.SECONDS); + + assertTrue(toolHandlerCalled[0], "Tool handler should have been called"); + + List toolEvents = events.stream() + .filter(e -> e instanceof ToolExecutionCompleteEvent).map(e -> (ToolExecutionCompleteEvent) e) + .toList(); + assertFalse(toolEvents.isEmpty(), "Should have a tool.execution_complete event"); + + ToolExecutionCompleteEvent toolEvt = toolEvents.get(0); + assertFalse(toolEvt.getData().success(), "Tool execution should not be marked as successful"); + assertNotNull(toolEvt.getData().error(), "Should have error details"); + assertEquals("denied", toolEvt.getData().error().code(), "Error code should be 'denied'"); + + session.close(); + } + } +} diff --git a/src/test/java/com/github/copilot/sdk/ToolsTest.java b/src/test/java/com/github/copilot/sdk/ToolsTest.java index d13db3685..6cd0c99bd 100644 --- a/src/test/java/com/github/copilot/sdk/ToolsTest.java +++ b/src/test/java/com/github/copilot/sdk/ToolsTest.java @@ -360,4 +360,109 @@ void testOverridesBuiltInToolWithCustomTool() throws Exception { session.close(); } } + + /** + * Verifies that the model can call multiple custom tools in parallel within a + * single turn. + * + * @see Snapshot: + * tools/should_execute_multiple_custom_tools_in_parallel_single_turn + */ + @Test + void testShouldExecuteMultipleCustomToolsInParallelSingleTurn() throws Exception { + ctx.configureForTest("tools", "should_execute_multiple_custom_tools_in_parallel_single_turn"); + + var toolACalled = new CompletableFuture(); + var toolBCalled = new CompletableFuture(); + + Map cityParams = Map.of("type", "object", "properties", + Map.of("city", Map.of("type", "string", "description", "City name")), "required", List.of("city")); + Map countryParams = Map.of("type", "object", "properties", + Map.of("country", Map.of("type", "string", "description", "Country name")), "required", + List.of("country")); + + ToolDefinition lookupCity = ToolDefinition.create("lookup_city", "Looks up city information", cityParams, + (invocation) -> { + String city = (String) invocation.getArguments().get("city"); + toolACalled.complete(city); + return CompletableFuture.completedFuture("CITY_" + city.toUpperCase()); + }); + + ToolDefinition lookupCountry = ToolDefinition.create("lookup_country", "Looks up country information", + countryParams, (invocation) -> { + String country = (String) invocation.getArguments().get("country"); + toolBCalled.complete(country); + return CompletableFuture.completedFuture("COUNTRY_" + country.toUpperCase()); + }); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client.createSession(new SessionConfig() + .setTools(List.of(lookupCity, lookupCountry)).setOnPermissionRequest(PermissionHandler.APPROVE_ALL)) + .get(); + + AssistantMessageEvent response = session.sendAndWait(new MessageOptions().setPrompt( + "Use lookup_city with 'Paris' and lookup_country with 'France' at the same time, then combine both results in your reply.")) + .get(60, TimeUnit.SECONDS); + + // Both tools should have been called + assertEquals("Paris", toolACalled.get(10, TimeUnit.SECONDS)); + assertEquals("France", toolBCalled.get(10, TimeUnit.SECONDS)); + + assertNotNull(response); + String content = response.getData().content(); + assertTrue(content.contains("CITY_PARIS"), "Response should contain CITY_PARIS: " + content); + assertTrue(content.contains("COUNTRY_FRANCE"), "Response should contain COUNTRY_FRANCE: " + content); + + session.close(); + } + } + + /** + * Verifies that excludedTools are respected even when also listed in + * availableTools. + * + * @see Snapshot: tools/should_respect_availabletools_and_excludedtools_combined + */ + @Test + void testShouldRespectAvailableToolsAndExcludedToolsCombined() throws Exception { + ctx.configureForTest("tools", "should_respect_availabletools_and_excludedtools_combined"); + + var excludedToolCalled = new boolean[]{false}; + + Map inputParams = Map.of("type", "object", "properties", + Map.of("input", Map.of("type", "string", "description", "Input value")), "required", List.of("input")); + + ToolDefinition allowedTool = ToolDefinition.create("allowed_tool", "An allowed tool", inputParams, + (invocation) -> { + String input = (String) invocation.getArguments().get("input"); + return CompletableFuture.completedFuture("ALLOWED_" + input.toUpperCase()); + }); + + ToolDefinition excludedTool = ToolDefinition.create("excluded_tool", "A tool that should be excluded", + inputParams, (invocation) -> { + excludedToolCalled[0] = true; + String input = (String) invocation.getArguments().get("input"); + return CompletableFuture.completedFuture("EXCLUDED_" + input.toUpperCase()); + }); + + try (CopilotClient client = ctx.createClient()) { + CopilotSession session = client.createSession(new SessionConfig() + .setTools(List.of(allowedTool, excludedTool)) + .setAvailableTools(List.of("allowed_tool", "excluded_tool")) + .setExcludedTools(List.of("excluded_tool")).setOnPermissionRequest(PermissionHandler.APPROVE_ALL)) + .get(); + + AssistantMessageEvent response = session + .sendAndWait(new MessageOptions() + .setPrompt("Use the allowed_tool with input 'test'. Do NOT use excluded_tool.")) + .get(60, TimeUnit.SECONDS); + + assertNotNull(response); + assertTrue(response.getData().content().contains("ALLOWED_TEST"), + "Response should contain ALLOWED_TEST: " + response.getData().content()); + assertFalse(excludedToolCalled[0], "Excluded tool should not have been called"); + + session.close(); + } + } } From 0a2157c161910ca603dee038fb501b10898c3683 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 5 May 2026 01:20:25 +0000 Subject: [PATCH 3/3] Update .lastmerge to c063458ecc3d606766f04cf203b11b08de672cc8, sync pom.xml CLI version, and update scripts/codegen @github/copilot version Co-authored-by: edburns <75821+edburns@users.noreply.github.com> --- .lastmerge | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.lastmerge b/.lastmerge index f0d3f0289..4a35fe8da 100644 --- a/.lastmerge +++ b/.lastmerge @@ -1 +1 @@ -ced6613253a595769d2e77547f9e1caf6bef6438 +c063458ecc3d606766f04cf203b11b08de672cc8