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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CI workflow for the feature/3.0.x line (JDK 21)
#
# Triggers:
# - push / pull_request on the feature/3.0.x branch
# - push / pull_request on the feature/3.0.x and test/20261118 branches
# - manual workflow_dispatch
#
# Runs `./mvnw -B clean verify` (Maven 4 via the checked-in wrapper — the
Expand All @@ -11,9 +11,9 @@ name: CI

on:
push:
branches: [feature/3.0.x]
branches: [feature/3.0.x, test/20261118]
pull_request:
branches: [feature/3.0.x]
branches: [feature/3.0.x, test/20261118]
workflow_dispatch:

permissions:
Expand Down
Empty file modified mvnw
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<!-- ═══ 第一段: 基础属性(自然排序)═══ -->
<java.version>21</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
<argLine></argLine>
<maven.version>4.0.0-rc-5</maven.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Dependency versions -->
Expand Down Expand Up @@ -239,7 +240,7 @@
<configuration>
<skip>false</skip>
<skipTests>false</skipTests>
<argLine>-Xmx1024m -Dfile.encoding=UTF-8</argLine>
<argLine>@{argLine} -Xmx1024m -Dfile.encoding=UTF-8</argLine>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/target/test-classes</additionalClasspathElement>
</additionalClasspathElements>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CodexAppServerRpc implements WebSocket.Listener {
private final StringBuilder frameBuffer = new StringBuilder();

private volatile WebSocket webSocket;
private volatile CodexWebSocketSender sender;
private volatile boolean mainRequestSent;

CodexAppServerRpc(CodexAppServerConfig config,
Expand Down Expand Up @@ -116,6 +117,7 @@ CompletableFuture<JsonNode> start(HttpClient httpClient) {
@Override
public void onOpen(WebSocket socket) {
this.webSocket = socket;
this.sender = new CodexWebSocketSender(socket);
socket.request(1);
// Tolerant initialize handshake: the app-server spec requires it, but
// older daemons may not implement it — an error response is logged and
Expand Down Expand Up @@ -223,7 +225,12 @@ private void sendRpc(long id, String rpcMethod, Map<String, Object> rpcParams) {
? codexError : new CodexAppServerException("Codex RPC failed", error));
return null;
});
sendText(toJson(payload));
sendText(toJson(payload)).whenComplete((ignored, error) -> {
if (Objects.nonNull(error)) {
pendingRpcs.remove(id, rpc);
fail(new CodexAppServerException("Codex RPC send failed", error));
}
});
}

private void handleInitializeSettled() {
Expand All @@ -238,7 +245,7 @@ private void handleInitializeSettled() {
payload.put("jsonrpc", "2.0");
payload.put("method", CodexAppServerProtocol.INITIALIZED);
payload.put("params", java.util.Collections.emptyMap());
socket.sendText(toJson(payload), true).whenComplete((w, error) -> {
sendText(toJson(payload)).whenComplete((w, error) -> {
if (Objects.nonNull(error)) {
fail(new CodexAppServerException("Codex initialized notification failed", error));
return;
Expand All @@ -247,11 +254,13 @@ private void handleInitializeSettled() {
});
}

private void sendText(String text) {
WebSocket socket = webSocket;
if (Objects.nonNull(socket)) {
socket.sendText(text, true);
private CompletionStage<WebSocket> sendText(String text) {
CodexWebSocketSender currentSender = sender;
if (Objects.isNull(currentSender)) {
return CompletableFuture.failedFuture(
new CodexAppServerException("Codex WebSocket is not connected"));
}
return currentSender.send(text);
}

private void fail(Throwable error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.easy4j.codex.appserver;

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

import java.lang.reflect.Proxy;
import java.net.http.WebSocket;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.Test;

import tools.jackson.databind.json.JsonMapper;

class CodexAppServerRpcSendTest {

@Test
void queuesInitializedAndMainRequestUntilPreviousWebSocketSendCompletes() throws Exception {
List<String> sent = new ArrayList<>();
Queue<CompletableFuture<WebSocket>> gates = new ArrayDeque<>();
CompletableFuture<WebSocket> initialize = new CompletableFuture<>();
CompletableFuture<WebSocket> initialized = new CompletableFuture<>();
CompletableFuture<WebSocket> main = new CompletableFuture<>();
gates.add(initialize);
gates.add(initialized);
gates.add(main);
AtomicBoolean pending = new AtomicBoolean();
WebSocket socket = (WebSocket) Proxy.newProxyInstance(
WebSocket.class.getClassLoader(), new Class<?>[]{WebSocket.class},
(proxy, method, args) -> {
if ("sendText".equals(method.getName())) {
if (!pending.compareAndSet(false, true)) {
throw new IllegalStateException("Send pending");
}
sent.add((String) args[0]);
CompletableFuture<WebSocket> gate = gates.remove();
return gate.thenApply(ignored -> {
pending.set(false);
return (WebSocket) proxy;
});
}
if ("request".equals(method.getName())) {
return proxy;
}
return null;
});

CodexAppServerRpc rpc = new CodexAppServerRpc(
new CodexAppServerConfig(), JsonMapper.builder().build(),
CodexAppServerProtocol.THREAD_LIST, Map.of());
rpc.onOpen(socket);
assertEquals(1, sent.size());

rpc.onText(socket, "{\"jsonrpc\":\"2.0\",\"id\":-1,\"result\":{}}", true);
assertEquals(1, sent.size(), "notification must wait for initialize send completion");

initialize.complete(socket);
assertEquals(2, sent.size());
initialized.complete(socket);
assertEquals(3, sent.size());
JsonMapper mapper = JsonMapper.builder().build();
assertEquals(CodexAppServerProtocol.INITIALIZE,
mapper.readTree(sent.get(0)).path("method").asText());
assertEquals(CodexAppServerProtocol.INITIALIZED,
mapper.readTree(sent.get(1)).path("method").asText());
assertEquals(CodexAppServerProtocol.THREAD_LIST,
mapper.readTree(sent.get(2)).path("method").asText());
main.complete(socket);
}
}
Loading