Skip to content

Commit 55b31d4

Browse files
committed
fix: gate reverse WebSocket collection on registration
1 parent d40f4de commit 55b31d4

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎openspec/changes/harden-opencli-reverse-agent/tasks.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@
2929
## 2.0 线的现有协议回归证据
3030

3131
`OpenCliWsReverseAgentSessionTest` 在回环接口启动独立 WebSocket 服务,验证注册默认值、心跳、异常帧后的继续处理、collect 参数到真实子进程的传递、错误结果,以及注册前和注册后断线重连。JDK 17 `mvn -B -ntp -o clean verify` 通过 2,092 项测试,零失败、零错误、零跳过。JaCoCo XML 记录 2,806 / 3,067 行覆盖(91.49%)。准入、背压、去重、完整生命周期和三线验收仍由上面的任务跟踪。
32+
33+
OC-WS-001 的提前收单场景使用子进程启动标记验证。旧实现收到 `registered` 之前的 collect 后会创建标记,定向用例因此失败。修复后同一回环请求收到明确失败结果,临时目录里没有启动标记。已注册成功的收单行为继续通过回归。JDK 17 `clean verify` 共通过 2,093 项测试,零跳过,JaCoCo 行覆盖率为 2,810 / 3,071(91.50%)。

‎src/main/java/io/github/easy4j/opencli/center/ws/OpenCliWsReverseAgentClient.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ private void handleTextMessage(
294294
return;
295295
}
296296
if ("collect".equals(type)) {
297+
if (shutdownRequested || !ws.isOpen() || registered.getCount() > 0
298+
|| Objects.nonNull(handshakeFailure.get())) {
299+
sendResult(ws, root.path("request_id").asText(""), false, Collections.emptyList(),
300+
"worker is not registered");
301+
return;
302+
}
297303
final JsonNode msg = root;
298304
collectPool.submit(() -> runCollectSafe(ws, msg));
299305
}

‎src/test/java/io/github/easy4j/opencli/center/ws/OpenCliWsReverseAgentSessionTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import io.github.easy4j.opencli.OpenCliProperties;
66
import io.github.easy4j.opencli.contract.ContractProbe;
7+
import io.github.easy4j.opencli.contract.LifecycleProbe;
78
import java.io.File;
89
import java.io.IOException;
910
import java.net.InetSocketAddress;
1011
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
1114
import java.util.Arrays;
1215
import java.util.Base64;
1316
import java.util.List;
@@ -22,6 +25,7 @@
2225
import org.java_websocket.handshake.ClientHandshake;
2326
import org.java_websocket.server.WebSocketServer;
2427
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.io.TempDir;
2529

2630
import static org.junit.jupiter.api.Assertions.assertEquals;
2731
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -89,6 +93,25 @@ void exchangesRegistrationAndCollectResultsAndReconnectsAfterRemoteClose() throw
8993
}
9094
}
9195

96+
@Test
97+
void collectBeforeRegistrationAcknowledgementDoesNotStartChild(@TempDir Path temporary) throws Exception {
98+
Path marker = temporary.resolve("child-started");
99+
try (LocalCenter center = new LocalCenter();
100+
OpenCliWsReverseAgentClient agent = new OpenCliWsReverseAgentClient(
101+
probe(LifecycleProbe.class.getName()), center.agentProperties())) {
102+
agent.start();
103+
WireMessage registration = center.receive("register");
104+
registration.socket.send(MAPPER.createObjectNode()
105+
.put("type", "collect").put("request_id", "early").put("site", "write")
106+
.put("command", marker.toString()).toString());
107+
JsonNode rejected = center.receive("result").body;
108+
assertEquals("early", rejected.path("request_id").asText());
109+
assertFalse(Files.exists(marker), "an unregistered collect started a child");
110+
assertFalse(rejected.path("success").asBoolean());
111+
assertEquals("worker is not registered", rejected.path("error").asText());
112+
}
113+
}
114+
92115
@Test
93116
void retriesAfterHandshakeCloseAndReturnsChildFailureToCenter() throws Exception {
94117
try (LocalCenter center = new LocalCenter();

0 commit comments

Comments
 (0)