Skip to content

Commit 5606eea

Browse files
committed
fix(cli): reject empty && chain operands in the plan validator (#1663)
splitOnUnquotedAnd() previously trimmed and filtered out empty segments, so a plan with a leading (`&& press ...`), trailing (`press ... &&`), or doubled (`a && && b`) operator passed validPlanCommands even though a real shell rejects all three as a syntax error. The validator would bless a plan that fails at execution. Empty segments are now surfaced as an `empty-chain-operand` issue instead of being silently dropped. The quoted-&& non-split behavior (label="A && B") is unchanged, and a normal single command with no chain still parses identically to before. Adds regression tests for all three empty-operand shapes plus the quoted-&& case.
1 parent 3c47adc commit 5606eea

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎scripts/__tests__/help-conformance-bench.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,38 @@ test('plan validator keeps single-command results identical when no chain is pre
421421
assert.equal(single.command, 'agent-device snapshot -i');
422422
});
423423

424+
// A real shell rejects && with an empty operand on either side. A validator
425+
// that silently dropped the empty segment (instead of failing it) would
426+
// bless a plan that fails at execution — exactly the gap review found.
427+
test('plan validator rejects a leading && as an empty chain operand', async () => {
428+
const [empty, closeSegment] = await validatePlanCommands(['&& agent-device close']);
429+
assert.equal(empty.issues[0]?.kind, 'empty-chain-operand');
430+
assert.equal(closeSegment.issues.length, 0);
431+
});
432+
433+
test('plan validator rejects a trailing && as an empty chain operand', async () => {
434+
const [pressSegment, empty] = await validatePlanCommands(['agent-device press @e1 --settle &&']);
435+
assert.equal(pressSegment.issues.length, 0);
436+
assert.equal(empty.issues[0]?.kind, 'empty-chain-operand');
437+
});
438+
439+
test('plan validator rejects a doubled && as an empty chain operand', async () => {
440+
const [openSegment, empty, closeSegment] = await validatePlanCommands([
441+
'agent-device open foo && && agent-device close',
442+
]);
443+
assert.equal(openSegment.issues.length, 0);
444+
assert.equal(empty.issues[0]?.kind, 'empty-chain-operand');
445+
assert.equal(closeSegment.issues.length, 0);
446+
});
447+
448+
test('plan validator still allows a quoted && to pass through a single segment unsplit', async () => {
449+
const [single] = await validatePlanCommands([
450+
'agent-device fill \'label="A && B"\' "value" --settle',
451+
]);
452+
assert.equal(single.issues.length, 0);
453+
assert.deepEqual(single.tokens, ['agent-device', 'fill', 'label="A && B"', 'value', '--settle']);
454+
});
455+
424456
test('case matchers score parsed tokens so shell quoting does not change results', async () => {
425457
const commands = [
426458
'agent-device open "com.example.shop"',

‎scripts/help-conformance-plan-validator.mjs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,31 @@ function applyCommandPolicy(parsed, agentResultState, allowedExternalCommands) {
4444
// full agent-device invocation, rather than the whole line failing as one
4545
// shell-projection violation. A `&&` inside a quoted selector value (for
4646
// example label="A && B") is not a chain boundary and must not split.
47+
//
48+
// A real shell rejects `&&` with an empty operand on either side (leading
49+
// `&& foo`, trailing `foo &&`, or doubled `foo && && bar`): each is a syntax
50+
// error, not two commands. The splitter below produces an empty segment for
51+
// exactly those shapes, so parseChainSegment turns an empty (post-trim)
52+
// segment into a validation issue instead of silently dropping it — a plan
53+
// with one of these shapes must not be blessed by validPlanCommands when it
54+
// would fail at execution.
4755
function parseCommandLine(command) {
48-
return splitOnUnquotedAnd(command).map((segment) => parsePlanCommand(segment));
56+
return splitOnUnquotedAnd(command).map((segment) => parseChainSegment(segment));
57+
}
58+
59+
function parseChainSegment(segment) {
60+
if (segment.trim().length > 0) return parsePlanCommand(segment.trim());
61+
return {
62+
command: segment,
63+
tokens: [],
64+
issues: [
65+
{
66+
kind: 'empty-chain-operand',
67+
error:
68+
'A && chain must have a non-empty command on both sides (no leading, trailing, or doubled &&).',
69+
},
70+
],
71+
};
4972
}
5073

5174
function splitOnUnquotedAnd(command) {
@@ -54,7 +77,7 @@ function splitOnUnquotedAnd(command) {
5477
index = consumeSplitCharacter(command, index, state);
5578
}
5679
state.segments.push(state.current);
57-
return state.segments.map((segment) => segment.trim()).filter((segment) => segment.length > 0);
80+
return state.segments;
5881
}
5982

6083
function consumeSplitCharacter(command, index, state) {

0 commit comments

Comments
 (0)