Skip to content

Commit 09a8f60

Browse files
committed
Correlate local OpenCLI failure diagnostics without payloads
1 parent d6e2da5 commit 09a8f60

5 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎.github/workflows/contracts.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ jobs:
131131
--suite process=io.github.easy4j.opencli.core.support.SubprocessCleanupFaultTest \
132132
--suite process=io.github.easy4j.opencli.core.support.SubprocessCompletionRaceTest \
133133
--suite process=io.github.easy4j.opencli.core.support.SubprocessStreamFailureTest \
134+
--suite process=io.github.easy4j.opencli.core.OpenCliProcessPrivacyTest \
134135
--suite protocol=io.github.easy4j.opencli.center.ws.OpenCliWsReverseAgentSessionTest \
135136
--suite protocol=io.github.easy4j.opencli.center.ws.OpenCliWsCollectRequestsTest \
136137
--suite protocol=io.github.easy4j.opencli.remote.OpenCliRemoteAgentHttpClientTest \

‎openspec/changes/harden-opencli-diagnostics-privacy/tasks.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ JDK 17 两项定向红灯实际退出 1。选项对象的 `toString()` 抛出带
4747
| JSON 解析 DEBUG、默认不写入的有界导出 | `OpenCliStdoutJsonTest`、`OpenCliDiagnosticExportTest` |
4848
| 启动日志的可用性摘要,配置和探测原始值的显式读取 | `OpenCliAvailabilityReportTest` |
4949

50-
JDK 17 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,134 项通过,0 失败、0 错误、0 跳过。中英文 README 记录应用显式访问原始低层诊断的责任。C03 的 YAML 解码器仍待实施,对其解析日志的验证保留在后续矩阵中。
50+
JDK 17 使用系统 Maven 执行 `-B -ntp -o clean verify`,2,134 项通过,0 失败、0 错误、0 跳过。中英文 README 记录应用显式访问原始低层诊断的责任。畸形 YAML 的验证见 C03 的隐私回归及下文。
5151

5252
## 启动诊断摘要验证
5353

5454
JDK 17 基线 `0cbb3ee750062675813f1d3389c5f7f14b50345c` 的定向用例在 3 项中失败 1 项。面向日志的 `toDiagnosticMessage()` 输出了配置值、解析后的可执行文件路径和原始探测消息。修改后,该方法只输出就绪状态和状态码。用例继续断言 `getConfiguredExecutable()`、`getResolvedExecutablePath()` 和 `getMessage()` 保留原始值。
5555

5656
三条线使用其 Maven Wrapper 运行 `./mvnw -B -ntp clean verify`,报告位于各自的 `target/surefire-reports`。Java 8 提交 `49250f97cb6618502b40e5989b8521fc733d7fab`、Java 17 本提交和 Java 21 提交 `7001ed85439408f9d2724e7edee92d3cfc7853e9` 分别通过 2178、2179、2178 项,均为零失败、零错误、零跳过。
57+
58+
59+
## 本地失败诊断关联
60+
61+
OC-PRIV-001/004。OpenCliProcessPrivacyTest 的新用例在 Java 17 基线复现缺少 correlationId,退出 1。现在本地进程失败时由 SDK 生成随机 UUID,在 DEBUG 日志及返回的异常消息中使用同一值。日志只记录终止原因、退出码和捕获字节数。测试扫描捕获的日志和完整异常栈,同时通过 partialResult 校验 stderr 原值。C03 的 OpenCliErrorEnvelopeTest 覆盖含敏感标记的畸形 YAML,并确认解析器原文不进入默认诊断。
62+
63+
Java 8、17、21 分别用各自 Wrapper 运行 OpenCliProcessPrivacyTest、OpenCliErrorEnvelopeTest 和 OpenCliProcessContractTest,每条线执行 31 项,零失败、零错误、零跳过。报告位于 target/surefire-reports。Java 21 的先前运行曾出现继承管道用例的 JUnit 临时目录清理竞态,本次重跑通过。该竞态需要独立处理。

‎src/main/java/io/github/easy4j/opencli/core/OpenCliExecutor.java‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import java.util.Locale;
2424
import java.util.Map;
2525
import java.util.Objects;
26+
import java.util.UUID;
2627
import lombok.Getter;
28+
import lombok.extern.slf4j.Slf4j;
2729
import org.apache.commons.exec.CommandLine;
2830

2931
/**
@@ -35,6 +37,7 @@
3537
* @since 3.0.0
3638
*/
3739
@Getter
40+
@Slf4j
3841
public class OpenCliExecutor implements AutoCloseable {
3942
private final OpenCliProperties properties;
4043
private final OpenCliProcessRuntime processRuntime;
@@ -198,8 +201,10 @@ private int effectiveCaptureLimit(int streamLimit) {
198201
}
199202

200203
private OpenCliResult complete(SubprocessExecutionSupport.RunSession session) {
201-
String stdout = new String(session.getStdout().toByteArray(), StandardCharsets.UTF_8);
202-
String stderr = new String(session.getStderr().toByteArray(), StandardCharsets.UTF_8);
204+
byte[] stdoutBytes = session.getStdout().toByteArray();
205+
byte[] stderrBytes = session.getStderr().toByteArray();
206+
String stdout = new String(stdoutBytes, StandardCharsets.UTF_8);
207+
String stderr = new String(stderrBytes, StandardCharsets.UTF_8);
203208
OpenCliExecutionDetails details = session.getExecutionDetails();
204209
TerminationReason reason = details.getTerminationReason();
205210
Integer exit = session.getObservedExitCode();
@@ -211,18 +216,24 @@ private OpenCliResult complete(SubprocessExecutionSupport.RunSession session) {
211216
.errorEnvelope(success ? null : OpenCliErrorDecoder.decode(stderr, exit,
212217
reason != TerminationReason.PROCESS_EXIT || !details.isStreamsDrained() || details.isStderrTruncated()))
213218
.build();
214-
if (success) { return result; }
219+
if (success) {
220+
return result;
221+
}
222+
String correlationId = UUID.randomUUID().toString();
223+
String diagnosticSuffix = " correlationId=" + correlationId;
224+
log.debug("OpenCLI process failed correlationId={} reason={} exitCode={} stdoutBytes={} stderrBytes={}",
225+
correlationId, reason, exit, stdoutBytes.length, stderrBytes.length);
215226
if (reason == TerminationReason.QUEUE_TIMEOUT || reason == TerminationReason.EXECUTION_TIMEOUT) {
216-
throw new OpenCliTimeoutException("OpenCLI deadline exceeded: " + reason, result);
227+
throw new OpenCliTimeoutException("OpenCLI deadline exceeded: " + reason + diagnosticSuffix, result);
217228
}
218229
if (reason == TerminationReason.SPAWN_FAILED) {
219-
throw new OpenCliExecutableFailureException("OpenCLI process could not be started", null, result);
230+
throw new OpenCliExecutableFailureException("OpenCLI process could not be started" + diagnosticSuffix, null, result);
220231
}
221232
if (reason == TerminationReason.PROCESS_EXIT && exit != null && exit != 0) {
222-
throw new OpenCliNonZeroExitException("OpenCLI returned nonzero exitCode=" + exit, result);
233+
throw new OpenCliNonZeroExitException("OpenCLI returned nonzero exitCode=" + exit + diagnosticSuffix, result);
223234
}
224235
// Native I/O errors may include executable paths or other invocation data.
225-
throw new OpenCliException("OpenCLI execution ended: " + reason, result);
236+
throw new OpenCliException("OpenCLI execution ended: " + reason + diagnosticSuffix, result);
226237
}
227238

228239
private File resolveWorkingDirectory() {

‎src/test/java/io/github/easy4j/opencli/core/OpenCliProcessPrivacyTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ void preservesStderrOnNonzeroExitWithoutLeakingArgsOrStderr() throws Exception {
4848
}
4949
}
5050

51+
@Test
52+
void correlatesLocalFailureLogAndStackWithoutPayload() throws Exception {
53+
String canary = "private-stderr-" + UUID.randomUUID();
54+
ByteArrayOutputStream output = new ByteArrayOutputStream();
55+
PrintStream original = System.err;
56+
OpenCliNonZeroExitException failure;
57+
try (OpenCliExecutor executor = new OpenCliExecutor(childProperties());
58+
PrintStream capture = new PrintStream(output, true, StandardCharsets.UTF_8.name())) {
59+
System.setErr(capture);
60+
try {
61+
failure = assertThrows(OpenCliNonZeroExitException.class,
62+
() -> executor.invoke("privacy", "nonzero", canary));
63+
failure.printStackTrace(capture);
64+
} finally {
65+
System.setErr(original);
66+
}
67+
}
68+
69+
String message = failure.getMessage();
70+
String marker = "correlationId=";
71+
int markerStart = message.indexOf(marker);
72+
assertTrue(markerStart >= 0, message);
73+
UUID id = UUID.fromString(message.substring(markerStart + marker.length()));
74+
String diagnostics = output.toString(StandardCharsets.UTF_8.name());
75+
assertTrue(diagnostics.contains("OpenCLI process failed " + marker + id
76+
+ " reason=PROCESS_EXIT exitCode=7 stdoutBytes=0 stderrBytes=" + canary.length()));
77+
assertFalse(diagnostics.contains(canary));
78+
assertEquals(canary, failure.getPartialResult().getStderr());
79+
}
80+
5181
@Test
5282
void adapterDebugLogsOnlyArgumentCount() throws Exception {
5383
String canary = "adapter-value-" + UUID.randomUUID();

‎src/test/resources/simplelogger.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ org.slf4j.simpleLogger.log.io.github.easy4j.opencli.parser.OpenCliStdoutJson=deb
22
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.browser.OpenCliBrowserSession=debug
33
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.remote.OpenCliArgvToCollectParser=debug
44
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.core.OpenCliAdapterChannel=debug
5+
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.core.OpenCliExecutor=debug

0 commit comments

Comments
 (0)