Skip to content

Commit f7df713

Browse files
committed
fix(acp): preserve terminal state across connect races
1 parent 91e3d97 commit f7df713

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎src/main/java/io/github/easy4j/kimi/acp/KimiAcpClient.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public String connect() {
137137
process = builder.start();
138138
} catch (IOException e) {
139139
connected.set(false);
140-
state.set(KimiAcpState.NEW);
140+
state.compareAndSet(KimiAcpState.CONNECTING, KimiAcpState.NEW);
141141
throw new KimiException("Failed to spawn kimi acp: " + config.getLocalExecutable(), e);
142142
}
143143
state.set(KimiAcpState.INITIALIZING);
@@ -158,7 +158,10 @@ public String connect() {
158158
if (result.hasNonNull("agentInfo")) {
159159
agentVersion = result.path("agentInfo").path("version").asText(null);
160160
}
161-
state.set(KimiAcpState.READY);
161+
if (!state.compareAndSet(KimiAcpState.INITIALIZING, KimiAcpState.READY)) {
162+
throw new KimiException("kimi acp initialize completed after lifecycle left INITIALIZING: "
163+
+ state.get());
164+
}
162165
return agentVersion;
163166
} catch (RuntimeException e) {
164167
// Handshake failure leaves the child alive — destroy it here so a
@@ -170,7 +173,7 @@ public String connect() {
170173
process = null;
171174
stdin = null;
172175
connected.set(false);
173-
state.set(KimiAcpState.NEW);
176+
state.compareAndSet(KimiAcpState.INITIALIZING, KimiAcpState.NEW);
174177
throw e;
175178
}
176179
}

0 commit comments

Comments
 (0)