Skip to content

Commit b480d6f

Browse files
committed
Reject valueless options before legacy OpenCLI collect
1 parent ebeb4de commit b480d6f

7 files changed

Lines changed: 21 additions & 17 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Notes:
192192

193193
- In remote mode the `stdout` is the JSON `items` the agent returned (parsed into line items per `format`), which may differ from local raw subprocess text; `leadingArguments` are local-only and are not injected on the remote path.
194194
- A legacy HTTP `/collect` reply must contain a JSON boolean `success`. Invalid envelopes raise `OpenCliRemoteProtocolException`, which extends `OpenCliExecutableFailureException`. A valid reply with `success: true` and omitted or null `items` still returns `[]`.
195-
- Legacy HTTP argv dispatch accepts SDK-listed adapters and one subcommand. Root metadata, Browser sessions, external passthrough, repeated options, `--`, and tokens whose spacing or leading dash cannot be preserved are rejected before HTTP. Place positionals before options. Use local execution for commands that need the original argv. A deployment with its own adapter contract can use the explicit `OpenCliCollectRequest` API.
195+
- Legacy HTTP argv dispatch accepts SDK-listed adapters and one subcommand. Root metadata, Browser sessions, external passthrough, repeated options, `--`, and tokens whose spacing or leading dash cannot be preserved are rejected before HTTP. Place positionals before options and supply an explicit value for each option. The legacy server appends a value to every option, so bare flags require local execution. Use local execution for commands that need the original argv. A deployment with its own adapter contract can use the explicit `OpenCliCollectRequest` API.
196196
- A collect timeout raises `OpenCliRemoteTimeoutException`. After dispatch to the HTTP transport, `isRemoteOutcomeUnknown()` is true. Collect does not automatically retry or follow redirects.
197197
- Inspect `getRemoteExecutionDetails()` on the result or on an exception’s `getPartialResult()` to distinguish HTTP status, Agent business status and the source of `items`. The legacy 0/1 returned by `getExitCode()` is synthetic. HTTP and protocol failure snapshots have no exit code. Accepted response text is available in those snapshots only when raw capture is enabled.
198198
- If a collect connection breaks before a complete response, the failure snapshot reports `TRANSPORT_FAILURE` and `isRemoteOutcomeUnknown()`. `getObservedHttpStatus()` is empty when no headers arrived. The snapshot has no CLI exit code or incomplete response body. Server-specific extensions require a verified Agent contract.

‎README.zh-CN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ cli.gemini().deepResearch("topic",
191191

192192
- 远程模式下 `stdout` 为 Agent 返回的 `items` 的 JSON(已按 `format` 解析为行列表),与本地原始子进程文本可能不同;`leadingArguments` 等仅影响本地进程拼装,**远程路径不注入**。
193193
- 旧版 HTTP `/collect` 响应须包含 JSON 布尔字段 `success`。无效响应抛出 `OpenCliRemoteProtocolException`,它继承 `OpenCliExecutableFailureException`。当 `success: true` 且 `items` 缺失或为 null 时,仍返回 `[]`。
194-
- 旧版 HTTP argv 调用仅接收 SDK 清单中的适配器及其子命令。根命令、Browser 会话、外部程序透传、重复选项、`--` 以及无法保留边界空格或前导短横的参数会在发请求前被拒绝。位置参数应在选项前。需要原样传递 argv 时使用本地执行,已有自定义 Agent 契约时可用显式 `OpenCliCollectRequest` API。
194+
- 旧版 HTTP argv 调用仅接收 SDK 清单中的适配器及其子命令。根命令、Browser 会话、外部程序透传、重复选项、`--` 以及无法保留边界空格或前导短横的参数会在发请求前被拒绝。位置参数应在选项前,每个选项必须显式带值。旧版服务会给每个选项补一个值,裸开关应使用本地执行。需要原样传递 argv 时使用本地执行,已有自定义 Agent 契约时可用显式 `OpenCliCollectRequest` API。
195195
- 远程调用超时抛出 `OpenCliRemoteTimeoutException`。请求提交给 HTTP 传输后,`isRemoteOutcomeUnknown()` 为 true。客户端不会自动重试或跟随重定向。
196196
- 通过结果或异常的 `getPartialResult()` 调用 `getRemoteExecutionDetails()`,查看 HTTP 状态、Agent 业务状态和 `items` 的来源。`getExitCode()` 的 0/1 是兼容映射。HTTP 或协议失败快照没有退出码。仅在开启原文捕获时,失败快照才携带已通过字节上限检查的响应正文。
197197
- collect 连接在完整响应到达前中断时,失败快照标记 `TRANSPORT_FAILURE`,`isRemoteOutcomeUnknown()` 为 true。未收到响应头时,`getObservedHttpStatus()` 为空。快照不包含 CLI 退出码或不完整响应正文。服务端扩展须经过对应 Agent 契约验证。

‎openspec/changes/harden-opencli-remote-collect/tasks.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ argv 入口继续限制为 SDK 已登记适配器和可保真转换的参数。
8888
实际 Agent 与 OpenCLI 1.8.8 的三线回环验收及默认端口兼容性差异见 [联调记录](evidence/2026-11-19-live-agent.md)。
8989

9090
同一实际 Agent 的三线 HTTPS、Authorization 及证书名称负例,也已写入 [联调记录](evidence/2026-11-19-live-agent.md#https-和显式-authorization)。
91+
92+
### OC-REMOTE-002 旧版 Agent 的无值选项
93+
94+
锁定的旧版 Agent 为每个 DTO 选项生成两个 argv 元素。原转换把尾部的 `--limit`、后接其他选项的 `--limit` 以及裸开关 `--verbose` 猜为布尔 true。JDK 17 回归中,该三项均错误进入 HTTP。转换现在要求明确的字符串值,拒绝向旧版协议派发裸选项。值明确的 `--limit 5`、`--limit=5` 和已联调的 `--help=true` 路径仍能转换。

‎src/main/java/io/github/easy4j/opencli/remote/OpenCliArgvToCollectParser.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ public static OpenCliCollectRequest parse(
6666
}
6767
String key = shortFormat ? "format" : requireIdentifier(option.substring(2));
6868
boolean isFormat = "format".equals(key) || "f".equals(key);
69-
Object value = Boolean.TRUE;
69+
String value;
7070
if (separator >= 0) {
7171
value = requireValue(token.substring(separator + 1));
72-
} else if (index < adapterAndRest.size()
73-
&& (Objects.isNull(adapterAndRest.get(index)) || !adapterAndRest.get(index).startsWith("-"))) {
72+
} else {
73+
if (index == adapterAndRest.size()) {
74+
throw new IllegalArgumentException("Legacy collect requires an explicit option value");
75+
}
7476
value = requireValue(adapterAndRest.get(index++));
7577
}
7678
if (isFormat) {
77-
if (formatSpecified || !(value instanceof String)) {
79+
if (formatSpecified) {
7880
throw new IllegalArgumentException("Legacy collect requires a single explicit format value");
7981
}
80-
format = (String) value;
82+
format = value;
8183
formatSpecified = true;
8284
} else if (Objects.nonNull(args.putIfAbsent(key, value))) {
8385
throw new IllegalArgumentException("Legacy collect cannot preserve a repeated option");

‎src/test/java/io/github/easy4j/opencli/remote/OpenCliArgvToCollectParserFullTest.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
class OpenCliArgvToCollectParserFullTest {
88

99
@Test
10-
void shouldParseDashDashFlagWithNextOption() {
11-
// --verbose followed by another flag means --verbose is boolean
12-
OpenCliCollectRequest req = OpenCliArgvToCollectParser.parse(
13-
Arrays.asList("npm", "search", "react", "--verbose", "--limit", "5"), "json", "cdp", "");
14-
assertEquals(Boolean.TRUE, req.getArgs().get("verbose"));
15-
assertEquals("5", req.getArgs().get("limit"));
10+
void shouldRejectAnOptionWithoutAValueBeforeAnotherOption() {
11+
assertThrows(IllegalArgumentException.class, () -> OpenCliArgvToCollectParser.parse(
12+
Arrays.asList("npm", "search", "react", "--limit", "--verbose"), "json", "cdp", ""));
1613
}
1714

1815
@Test

‎src/test/java/io/github/easy4j/opencli/remote/OpenCliArgvToCollectParserTest.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ void shouldParseEqualsOptions() {
6363
}
6464

6565
@Test
66-
void shouldParseBooleanFlags() {
67-
OpenCliCollectRequest req = OpenCliArgvToCollectParser.parse(
68-
Arrays.asList("npm", "search", "react", "--verbose"), "json", "cdp", "");
69-
assertEquals(Boolean.TRUE, req.getArgs().get("verbose"));
66+
void shouldRejectBareFlags() {
67+
assertThrows(IllegalArgumentException.class, () -> OpenCliArgvToCollectParser.parse(
68+
Arrays.asList("npm", "search", "react", "--verbose"), "json", "cdp", ""));
7069
}
7170

7271
@Test

‎src/test/java/io/github/easy4j/opencli/remote/OpenCliRemoteAgentHttpClientTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ void losslessAdapterRequestReachesLegacyEndpoint() throws IOException {
816816
@ValueSource(strings = {
817817
"browser|session-a|state", "list|-f|json", "external|list", "unverified-tool|version",
818818
"npm|search|--tag|one|--tag=two", "npm|search|--|react", "npm|search|--limit|-1",
819+
"npm|search|react|--limit", "npm|search|react|--limit|--bogus",
820+
"npm|search|react|--verbose",
819821
"npm|search|--limit=-1", "npm|search|", "npm|search|--query=", "npm|search|--query| padded ",
820822
"npm|search|-f|json|--format=yaml", "npm|search|--format|--verbose",
821823
"npm|search|-v", "npm|search|--limit|5|react", " npm|search"

0 commit comments

Comments
 (0)