Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions java/sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
mvn verify -Dcopilot.cli.path=/some/other/copilot/npm-loader.js
-->
<copilot.cli.path>${copilot.sdk.root}/nodejs/node_modules/@github/copilot/npm-loader.js</copilot.cli.path>
<!--
Path to the platform-specific Copilot CLI binary (not the
npm-loader.js dispatcher) used by the -Pinprocess profile: the
in-process FFI host loads runtime.node directly, so it needs the
real per-platform binary/prebuilds layout, not the thin loader
script. Override with -Dcopilot.inprocess.cli.path=... for other
platforms/architectures; Linux-x64 is the only supported platform
for InProcess E2E tests in this phase.
-->
<copilot.inprocess.cli.path>${copilot.sdk.root}/nodejs/node_modules/@github/copilot-linux-x64/copilot</copilot.inprocess.cli.path>
<!-- Set to true (via -Pskip-test-harness) to skip npm install of test harness -->
<skip.test.harness>false</skip.test.harness>
<!--
Expand Down Expand Up @@ -571,6 +581,63 @@ did not produce the multi-release output. Re-build on JDK 25+ and verify the
<skip.test.harness>true</skip.test.harness>
</properties>
</profile>
<!--
Runs the E2E suite over the in-process (FFI) transport instead of
the default child-process transport. COPILOT_SDK_DEFAULT_CONNECTION
makes CopilotClient host the runtime in-process (see
CopilotClient.resolveDefaultConnection). forkCount=1 and
parallel=none are required because InProcessEnvGuard and the FFI
host mutate process-global state (the native process environment
block and JNA callback registration).
-->
<profile>
<id>inprocess</id>
<properties>
<COPILOT_SDK_DEFAULT_CONNECTION>inprocess</COPILOT_SDK_DEFAULT_CONNECTION>
</properties>
<dependencies>
<!--
Classpath resource native/linux-x64/runtime.node, resolved by
NativeRuntimeLoader when no runtime.node is found next to
COPILOT_CLI_PATH. Test-scoped: production consumers add the
classifier they need themselves.
-->
<dependency>
<groupId>com.github</groupId>
<artifactId>copilot-sdk-java-runtime</artifactId>
<version>${project.version}</version>
<classifier>linux-x64</classifier>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Leave Surefire on its standard transport; only Failsafe ITs use InProcess -->
<skipTests>false</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
<parallel>none</parallel>
<includes>
<include>**/InProcessTransportIT.java</include>
</includes>
<environmentVariables>
<COPILOT_CLI_PATH>${copilot.inprocess.cli.path}</COPILOT_CLI_PATH>
<COPILOT_SDK_DEFAULT_CONNECTION>inprocess</COPILOT_SDK_DEFAULT_CONNECTION>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!--
Skip the install-nodejs-cli-dependencies (npm ci) execution when
tests are skipped via -DskipTests, so non-test builds do not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,31 @@ static Path resolve(String cliPathEnv, Path cacheBase, ClassLoader loader, Strin

/**
* Checks for {@code runtime.node} alongside the configured CLI.
*
* <p>
* Checks, in order, the flat bundled layout ({@code runtime.node} directly next
* to the CLI) and the npm package layout
* ({@code prebuilds/<classifier>/runtime.node} next to the CLI), matching the
* two layouts the {@code @github/copilot-<platform>} packages may ship.
*/
static Path resolveFromCliPath(String cliPathStr) throws IOException {
if (cliPathStr == null || cliPathStr.isBlank()) {
return null;
}
Path cliPath = Path.of(cliPathStr).toAbsolutePath().normalize();
Path parent = cliPath.getParent();
Path candidate = parent.resolve(RUNTIME_FILENAME);
if (Files.isRegularFile(candidate) && Files.size(candidate) > 0) {
return candidate;

Path flat = parent.resolve(RUNTIME_FILENAME);
if (Files.isRegularFile(flat) && Files.size(flat) > 0) {
return flat;
}

Path prebuilt = parent.resolve("prebuilds").resolve(PlatformDetector.detectClassifier())
.resolve(RUNTIME_FILENAME);
if (Files.isRegularFile(prebuilt) && Files.size(prebuilt) > 0) {
return prebuilt;
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/

package com.github.copilot.e2e;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Map;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.github.copilot.AllowCopilotExperimental;
import com.github.copilot.CopilotClient;
import com.github.copilot.E2ETestContext;
import com.github.copilot.ffi.InProcessEnvGuard;
import com.github.copilot.rpc.CopilotClientOptions;
import com.github.copilot.rpc.PingResponse;
import com.github.copilot.rpc.RuntimeConnection;

/**
* Failsafe integration test for the in-process (FFI) transport.
*
* <p>
* Loads the real {@code runtime.node} native library into this test process via
* {@link com.github.copilot.ffi.FfiRuntimeHost}, performs a purely local
* {@code ping} round-trip through the runtime, and stops cleanly. {@code ping}
* is answered by the runtime itself, so no auth or replay proxy is involved —
* this mirrors {@code nodejs/test/e2e/inprocess_ffi.e2e.test.ts},
* {@code go/internal/e2e/inprocess_ffi_e2e_test.go}, and
* {@code python/e2e/test_inprocess_ffi_e2e.py}.
*
* <p>
* {@link InProcessEnvGuard} demonstrates how the harness redirects the native
* runtime's HTTP traffic to the replay proxy (via {@code COPILOT_API_URL}) for
* tests that need session/message round trips over the in-process transport:
* the native library reads environment variables from the live OS process
* environment block, not from the JVM's {@code System.getenv()} snapshot, so
* only a JNA-backed native call can make it visible to code already loaded
* in-process.
*
* <p>
* Run with {@code mvn verify -Pinprocess} from the {@code java} reactor root,
* which builds the {@code copilot-sdk-java-runtime} artifact and sets
* {@code COPILOT_CLI_PATH} to the pinned CLI whose sibling {@code runtime.node}
* this test loads, and forces {@code forkCount=1} because the FFI host and env
* guard mutate process-global state.
*
* <p>
* {@link RequireInProcess} disables this test unless the {@code -Pinprocess}
* profile is active: without it, the {@code copilot-sdk-java-runtime}
* classifier JAR providing {@code runtime.node} is not on the classpath, so the
* test would fail with a {@code FileNotFoundException} rather than being
* skipped.
*/
@AllowCopilotExperimental
@RequireInProcess
class InProcessTransportIT {

private static E2ETestContext ctx;

@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}

@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}

@Test
void shouldStartPingAndStopOverInProcessFfi() throws Exception {
// Route the native runtime's HTTP traffic (should it make any) at the
// replay proxy, mirroring how a session-level in-process test would
// redirect COPILOT_API_URL. `ping` never reaches the network, but this
// demonstrates the guard's intended usage for future in-process tests.
// COPILOT_CLI_PATH is intentionally NOT set here: NativeRuntimeLoader and
// CopilotClient.resolveInProcessEntrypoint() read it via
// System.getenv(), which is a JVM-startup-time snapshot that native
// setenv() calls made after the JVM starts cannot update — it must be
// set before the JVM starts (see the -Pinprocess Maven profile).
try (InProcessEnvGuard envGuard = new InProcessEnvGuard(Map.of("COPILOT_API_URL", ctx.getProxyUrl()))) {
CopilotClientOptions options = new CopilotClientOptions().setConnection(RuntimeConnection.forInProcess());
try (CopilotClient client = new CopilotClient(options)) {
client.start().get();

PingResponse pong = client.ping("ffi message").get();
assertEquals("pong: ffi message", pong.message());
assertNotNull(pong.timestamp());
Comment thread
edburns marked this conversation as resolved.

client.stop().get();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/

package com.github.copilot.e2e;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* Enables an annotated test class or method only when the E2E suite runs under
* the in-process (FFI) transport, i.e. when
* {@code COPILOT_SDK_DEFAULT_CONNECTION} is set to {@code inprocess}.
*
* <p>
* Use this for tests that require the real {@code runtime.node} native library
* to be present on the classpath, which only the {@code -Pinprocess} Maven
* profile guarantees (see {@link InProcessTransportIT}). Without this profile,
* standard {@code mvn verify} runs would fail with a
* {@code FileNotFoundException} because the classifier JAR providing
* {@code runtime.node} is not on the classpath.
* </p>
*
* <p>
* The inverse of {@link SkipInProcess}.
* </p>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(RequireInProcess.Condition.class)
public @interface RequireInProcess {

/**
* Explains why the annotated test requires the in-process transport.
*
* @return the skip reason used when the in-process transport is not active
*/
String value() default "Requires the -Pinprocess Maven profile";

/**
* JUnit 5 execution condition backing {@link RequireInProcess}.
*/
final class Condition implements org.junit.jupiter.api.extension.ExecutionCondition {

private static final String DEFAULT_CONNECTION_ENV_VAR = "COPILOT_SDK_DEFAULT_CONNECTION";

@Override
public org.junit.jupiter.api.extension.ConditionEvaluationResult evaluateExecutionCondition(
org.junit.jupiter.api.extension.ExtensionContext context) {
String envValue = System.getenv(DEFAULT_CONNECTION_ENV_VAR);
if ("inprocess".equalsIgnoreCase(envValue)) {
return org.junit.jupiter.api.extension.ConditionEvaluationResult
.enabled("Running under the in-process transport");
}
String reason = context.getElement().map(element -> element.getAnnotation(RequireInProcess.class))
.map(RequireInProcess::value).orElse("Requires the -Pinprocess Maven profile");
return org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled(reason);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/

package com.github.copilot.e2e;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* Disables an annotated test class or method when the E2E suite runs under the
* in-process (FFI) transport, i.e. when {@code COPILOT_SDK_DEFAULT_CONNECTION}
* is set to {@code inprocess}.
*
* <p>
* Use this for tests that rely on per-client process settings the in-process
* transport cannot honor — for example per-client environment variables, since
* the in-process runtime shares the host process's single environment (see
* {@link com.github.copilot.rpc.InProcessRuntimeConnection} and
* <a href="https://github.com/github/copilot-sdk/issues/1934">issue #1934</a>).
* </p>
*
* <p>
* Mirrors {@code skip_inprocess(reason)} in the Rust E2E harness.
* </p>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(SkipInProcess.Condition.class)
public @interface SkipInProcess {

/**
* Explains why the annotated test is incompatible with the in-process
* transport.
*
* @return the skip reason
*/
String value() default "Not supported under the in-process (FFI) transport";

/**
* JUnit 5 execution condition backing {@link SkipInProcess}.
*/
final class Condition implements org.junit.jupiter.api.extension.ExecutionCondition {

private static final String DEFAULT_CONNECTION_ENV_VAR = "COPILOT_SDK_DEFAULT_CONNECTION";

@Override
public org.junit.jupiter.api.extension.ConditionEvaluationResult evaluateExecutionCondition(
org.junit.jupiter.api.extension.ExtensionContext context) {
String envValue = System.getenv(DEFAULT_CONNECTION_ENV_VAR);
if (!"inprocess".equalsIgnoreCase(envValue)) {
return org.junit.jupiter.api.extension.ConditionEvaluationResult
.enabled("Not running under the in-process transport");
}
String reason = context.getElement().map(element -> element.getAnnotation(SkipInProcess.class))
.map(SkipInProcess::value).orElse("Not supported under the in-process (FFI) transport");
return org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled(reason);
}
}
}
Loading
Loading