Skip to content

Commit e4022f1

Browse files
committed
Complete OpenCLI argv launcher and migration review
1 parent 4411a4b commit e4022f1

5 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Notes:
208208
- Local invocation preserves empty strings, padding, Unicode, line breaks, and literal option terminators. If your application relied on trimming or dropping values, normalize its inputs explicitly before calling the SDK.
209209
- Use the adapter's no-argument invocation or an empty list for a root command. A null varargs array or null token is invalid. An empty string token remains a valid value.
210210
- In the legacy options map, Boolean `false` still omits the option. Use `OpenCliOption.value` for a schema-supported literal `false`, or `OpenCliOption.negated` for a schema-supported negative flag. A negatable option reserves both its positive and negative names, so duplicate definitions across ordered and legacy options are rejected.
211-
- On Windows, use the full path to a native executable such as `node.exe`, with the installed OpenCLI JavaScript entry point in `leadingArguments`. The SDK rejects batch launchers. If the JVM is running in strict process mode, a literal double quote in a native executable argument is rejected before spawn with its index and an explanation.
211+
- On Windows, use the full path to a native executable such as `node.exe`, with the installed OpenCLI JavaScript entry point in `leadingArguments`. The SDK rejects batch launchers. If the JVM is running in strict process mode, a literal double quote in an argument to a native EXE or an extensionless launcher (the shell `cmd.exe` follows its own command contract) is rejected before spawn with its index and an explanation.
212212

213213
### 8.1 meta() and browser()
214214

‎README.zh-CN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ cli.gemini().deepResearch("topic",
207207
- 本地调用原样保留空字符串、边界空白、Unicode、换行和字面参数终止符。此前依赖 SDK 裁剪或丢弃参数的调用方,应在调用前显式处理自己的输入。
208208
- 调用适配器根命令时使用无参调用或空列表。varargs 数组与 token 不能为 null,空字符串仍是有效参数值。
209209
- 旧选项 Map 的布尔 `false` 继续表示省略。显式布尔值使用有对应 schema 的 `OpenCliOption.value`,否定选项使用 `OpenCliOption.negated`。可否定的结构化选项同时占用正向名和否定别名,避免与旧 Map 或其他结构化选项冲突。
210-
- Windows 使用 `node.exe` 等原生可执行文件的完整路径,并在 `leadingArguments` 中指定已安装的 OpenCLI JavaScript 入口。SDK 拒绝批处理入口。在 JVM 严格进程模式下,原生可执行文件参数中的字面双引号在启动前被明确拒绝,错误包含参数索引。
210+
- Windows 使用 `node.exe` 等原生可执行文件的完整路径,并在 `leadingArguments` 中指定已安装的 OpenCLI JavaScript 入口。SDK 拒绝批处理入口。在 JVM 严格进程模式下,原生 EXE 或无扩展名的启动名参数中的字面双引号(`cmd.exe` 遵循自身的命令契约)在启动前被明确拒绝,错误包含参数索引。
211211

212212
### 8.1 meta() 与 browser()
213213

‎openspec/changes/harden-opencli-argv-contract/tasks.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [x] 3.1 补 List/varargs/typed/request 等价与用户列表不被修改的测试。
2020
- [x] 3.2 检查 Windows launcher 与引号边界,未测试的平台明确保留未验证状态。
2121
- [x] 3.3 分别在三条 feature 线运行共享 vectors 和现有测试,记录允许差异。
22-
- [ ] 3.4 评审迁移说明与完整 diff;需求通过后再申请 archive,不在文档阶段勾选。
22+
- [x] 3.4 评审迁移说明与完整 diff;归档随最终集成验收申请。
2323

2424
## 证据交付模板(实施时填写)
2525

@@ -42,3 +42,10 @@
4242
独立评审复现了 Windows 严格进程模式下潜在的字面引号降级,以及可否定选项混用否定别名的冲突。Java 17 基线 `75160c6` 的两项定向测试均失败。原生 EXE 的严格模式会在取得子进程前报告无法保真的参数索引,消息不含值,普通参数仍可执行。可否定 schema 同时保留正向名和否定别名,两种排列顺序和旧 Map 均不会发出冲突组合。
4343

4444
修复后 Java 8、17、21 各用自己的 Wrapper 执行 `./mvnw -B -ntp -o clean verify`,分别通过 2,257、2,258、2,257 项,零失败、零错误、零跳过。仍待以新提交的 Windows 托管报告和独立复审作为 3.4 的最终依据。
45+
46+
47+
在上一轮候选 `a965392`、`4fa19df`、`4411a4b` 的三份 Windows 工件中,分别确认严格模式用例与否定别名用例真实执行并通过。随后独立复审发现严格模式还需覆盖名称以 `cmd.exe` 结尾的普通 EXE,以及无扩展名的原生启动名。启动检查现在按 basename 精确识别实际的 `cmd.exe`,以相同规则处理正反斜杠路径。两个路径变体和无扩展名用例均在模拟严格 Windows 启动器中断言未调用进程启动器。双语迁移说明已同步此边界。
48+
49+
独立复审确认两类问题已解决。最终三线本地 Wrapper 全量分别执行 2,260、2,261、2,260 项,失败、错误、跳过均为 0。再次比对目标分支 JVM 声明,原有的 151 个类型和 1,687 个成员仍保留。C01 技术评审完成,当前 PR 仍按 C02 与 C10 的独立门禁推进。
50+
51+
[Java 8 Windows 结果](https://github.com/easy-4-java/opencli-java-sdk/actions/runs/44409386904) · [Java 17 Windows 结果](https://github.com/easy-4-java/opencli-java-sdk/actions/runs/44409386886) · [Java 21 Windows 结果](https://github.com/easy-4-java/opencli-java-sdk/actions/runs/44409386943)

‎src/main/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupport.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,18 @@ synchronized boolean isPending() {
390390
// The Windows JDK legacy serializer treats a surrounding quote pair as already encoded.
391391
@SuppressWarnings("removal")
392392
private static List<String> processArguments(List<String> argv) {
393-
String executable = argv.get(0).toLowerCase(Locale.ROOT);
394-
if (!System.getProperty("os.name").startsWith("Windows") || !executable.endsWith(".exe")
395-
|| executable.endsWith("cmd.exe")) {
393+
if (!System.getProperty("os.name").startsWith("Windows")) {
396394
return argv;
397395
}
396+
String executable = argv.get(0).toLowerCase(Locale.ROOT);
397+
int separator = Math.max(executable.lastIndexOf('/'), executable.lastIndexOf('\\'));
398+
String name = executable.substring(separator + 1);
398399
boolean strict = Objects.nonNull(System.getSecurityManager())
399400
|| "false".equalsIgnoreCase(System.getProperty("jdk.lang.Process.allowAmbiguousCommands"));
401+
boolean nativeLaunch = name.endsWith(".exe") || strict && name.indexOf('.') < 0;
402+
if (!nativeLaunch || "cmd.exe".equals(name)) {
403+
return argv;
404+
}
400405
List<String> arguments = new ArrayList<>(argv);
401406
for (int i = 1; i < arguments.size(); i++) {
402407
String argument = arguments.get(i);

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupportTest.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.junit.jupiter.api.io.TempDir;
1414
import static org.junit.jupiter.api.Assertions.*;
1515
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.ValueSource;
1618

1719
@org.junit.jupiter.api.parallel.Isolated
1820
class SubprocessExecutionSupportTest {
@@ -88,8 +90,9 @@ void shouldKeepNativePathOutOfDefaultHandlerException(@TempDir Path temporary) t
8890
assertFalse(diagnostics.toString().contains(canary));
8991
}
9092

91-
@Test
92-
void rejectsQuotedWindowsArgumentsBeforeNativeSpawnInStrictMode() throws Exception {
93+
@ParameterizedTest
94+
@ValueSource(strings = {"fixture.exe", "C:\\tools\\fixturecmd.exe", "C:/tools/fixturecmd.exe", "node"})
95+
void rejectsQuotedWindowsArgumentsBeforeNativeSpawnInStrictMode(String executable) throws Exception {
9396
String previousOs = System.getProperty("os.name");
9497
String property = "jdk.lang.Process.allowAmbiguousCommands";
9598
String previousMode = System.getProperty(property);
@@ -98,10 +101,10 @@ void rejectsQuotedWindowsArgumentsBeforeNativeSpawnInStrictMode() throws Excepti
98101
launches.incrementAndGet();
99102
throw new IOException("fixture must not launch");
100103
}, timeout -> new org.apache.commons.exec.ExecuteWatchdog(timeout));
101-
CommandLine command = new CommandLine("fixture.exe") {
104+
CommandLine command = new CommandLine(executable) {
102105
@Override
103106
public String[] toStrings() {
104-
return new String[]{"fixture.exe", "\"private-quoted-value\""};
107+
return new String[]{executable, "\"private-quoted-value\""};
105108
}
106109
};
107110
SubprocessExecutionSupport.ExecutionRequest request =

0 commit comments

Comments
 (0)