From 2eb8da7bbe75b6e143d088fc425939bcabb024b0 Mon Sep 17 00:00:00 2001 From: Amm0Bot Date: Thu, 26 Nov 2026 16:05:51 -0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(explain):=20warn=20on=20an=20unknown=20?= =?UTF-8?q?or=20ambiguous=20command=20inside=20[=20=E2=80=A6=20]=20(#572)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `:put [/ip/address/nosuchverb]` and `:local v [nosuchcmd where name=x]` passed silently while the same text at the top level warned. The unresolved/ambiguous warnings were emitted per statement only. A bracket the verb reader read now gets the same abstention warning, spanned on the substitution. A bracket with no command head ([$f], [(…)]) stays silent: the device accepts those (corrected-anchors rows trailing-selector*). Corpus (948 scripts, pinned): 14 move pass -> warn; 3 are rejected by every recorded RouterOS version, 11 accepted and unreadable offline at the top level too (recorded as two matrix nits). Session: 4f3a1170-9bf0-4eac-97bd-7c9c9d0e6bbd Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 +++ GLOSSARY.txt | 1 + docs/MATRIX.md | 8 ++ src/explain.ts | 85 +++++++++++++-------- test/unit/explain-corrected-anchors.test.ts | 5 +- test/unit/explain-structure.test.ts | 43 +++++++++++ 6 files changed, 120 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da4ad4f9..7778de9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,16 @@ publish `0.2.0-next.` to `@next`; `@latest` stays on 0.1.10. ### Fixed +- `explain` warns on an unknown or ambiguous command inside `[ … ]` (#572), + the same `explain/canonicalizer/unresolved-statement` or + `ambiguous-statement` warning its top-level form already got, spanned on + the substitution: `:put [/ip/address/nosuchverb]` was `pass`. It stays a + warning, never an error. A bracket with no command head, such as `[$f]` or + `[($a . $b)]`, stays silent, as before. On the pinned 948-script corpus, + 14 scripts move from `pass` to `warn`: 3 that every recorded RouterOS + version rejects, and 11 it accepts, which the offline reader cannot place + at the top level either (colon-less builtins like `[typeof $b]`, bare + no-argument commands like `[/terminal/inkey]`). - `execute --via mac-telnet` returns the command's output without its echo (#575). The reader skipped as many screen columns as the command was long, but the console's echo differs from the bytes sent: it drops a trailing diff --git a/GLOSSARY.txt b/GLOSSARY.txt index 1992e8c8..166db023 100644 --- a/GLOSSARY.txt +++ b/GLOSSARY.txt @@ -869,3 +869,4 @@ COMSPEC # Windows command interpreter variable Wbem # Windows Management Instrumentation directory in the default Windows PATH NONBLOCK # O_NONBLOCK open flag: a password file never blocks on a FIFO mkfifo # POSIX command that makes a named pipe (FIFO), used in tests +inkey # RouterOS `/terminal/inkey`, which reads one key press diff --git a/docs/MATRIX.md b/docs/MATRIX.md index 6b83a2ad..7cc3b2e0 100644 --- a/docs/MATRIX.md +++ b/docs/MATRIX.md @@ -487,6 +487,14 @@ done or declined. A nit that needs more explanation is an issue. columns, and a wide table prints `Columns: -- (terminal too narrow)` instead of its rows. Logging in as `user+w` (honored by non-interactive SSH since 7.22; measured on CHR 7.23.8 and 7.24.5 for #676) keeps the columns. +- `explain` offline: colon-less builtins (`typeof $b`, `[pick $s 0 3]`) read + as `bare-word head is not a known verb` and warn, at the top level and in + brackets since #572, though RouterOS accepts them; 9 of the 11 + device-accepted scripts that #572 moved to `warn` are this shape or + `[/terminal/inkey]`. +- `explain` offline: `/system/script/run staticUrl` reads as `ambiguous` + "bare path, nothing follows", so a positional operand after `run` is taken + for a path segment (seen in the #572 corpus census). - `.github/instructions/`: trim Copilot-era verbosity when the TIKOCI-wide files are collected in `tikoci.github.io`. - Error fields: `docs/CONSTITUTION.md` → Error model specifies `message` and diff --git a/src/explain.ts b/src/explain.ts index f7a3d79b..1a1439c9 100644 --- a/src/explain.ts +++ b/src/explain.ts @@ -1611,6 +1611,14 @@ export function explainCommand( // segments and pairing the two by index attaches the wrong span to every statement after // the first block. Each split carries its own document-space span. // + // The verb reader warns on a bracket it read (#572). A bracket it never + // read — a `[$f]` call or a `[(…)]` expression has no command head — stays + // silent: the device accepts those (fixture rows `trailing-selector*`). + const bracketReadings = brackets.resolutions.map((r) => ({ + read: !bracketRefused(r), + subcommand: subcommandOf(r), + })); + const subcommands = bracketReadings.map(({ subcommand }) => subcommand); const readStatements: ExplainStatement[] = verbs.splits.map((split, index) => statementOf( split, @@ -1699,6 +1707,9 @@ export function explainCommand( pairs.filter((pair) => pair.valueShaped).map(spacedArgumentDiagnostic), ), ...statements.flatMap((s) => diagnosticsForStatement(s)), + ...bracketReadings.flatMap(({ read, subcommand }) => + read ? abstentionDiagnostics(subcommand) : [], + ), ].sort(compareExplainDiagnostics); const valueFacts = valuesOf( @@ -1800,7 +1811,7 @@ export function explainCommand( statements, blocks, containsWrite: renderWriteVerdict(write.verdict), - subcommands: brackets.resolutions.map(subcommandOf), + subcommands, // `containsWrite` is the fact `structure` itself asserts; the statements // and subcommands beside it carry their own. ev: EV.write, @@ -2707,23 +2718,27 @@ function enforceGateParity( return out; } +/** Q3 refused the bracket: it found no command head to read. */ +function bracketRefused(r: Resolution): boolean { + return r.unresolved !== undefined || r.path === null; +} + /** One `[…]` substitution in the envelope's vocabulary. See {@link ExplainSubcommand}. */ function subcommandOf(r: Resolution): ExplainSubcommand { // Q3 refused: the bracket is unreadable and Q6's reading of the same text // cannot promote it. - const reading = - r.unresolved !== undefined || r.path === null - ? { - resolution: "unknown" as const, - unresolved: r.unresolved ?? "no reading of this substitution", - } - : readingOf( - resolveVerb( - r.inner, - r.context, - rangeForResolution(r) ?? documentRange(r.inner), - ), - ); + const reading = bracketRefused(r) + ? { + resolution: "unknown" as const, + unresolved: r.unresolved ?? "no reading of this substitution", + } + : readingOf( + resolveVerb( + r.inner, + r.context, + rangeForResolution(r) ?? documentRange(r.inner), + ), + ); return { owner: `s${r.owner}`, span: { start: r.span.start, end: r.span.end }, @@ -2765,16 +2780,39 @@ function renderWriteVerdict(verdict: WriteVerdict): boolean | "unknown" { return verdict === "unknown" ? "unknown" : verdict === "true"; } +/** Per-statement diagnostics: the abstentions plus the #192 context signal. */ +function diagnosticsForStatement(s: ExplainStatement): ExplainDiagnostic[] { + const out = abstentionDiagnostics(s); + // The #192 signal: a reading that is CORRECT while the document context was + // already lost. Info severity — the statement resolved without consuming the + // context, so nothing about it is wrong; a consumer ranking readings or + // offering completions is the one that needs to know. + if (s.resolution === "resolved" && !s.contextCertain) + out.push({ + code: "explain/canonicalizer/context-lost", + severity: "info", + message: + "resolved without the document's menu context, which an earlier unreadable statement destroyed", + span: s.span, + ev: s.ev, + }); + return out; +} + /** - * Per-statement diagnostics — the abstentions, said out loud. + * The abstentions, said out loud — for a statement or a `[…]` subcommand alike. * * A refusal is a first-class result, not an error: `ambiguous` and `unknown` are * the ratified vocabulary for "the input has several valid schema-free readings" * and "analysis cannot safely recover one". They are WARNINGS, never errors, so * the default `--fail-on error` does not fail a document whose only sin is being - * unreadable without a schema — which is most of RouterOS scripting. + * unreadable without a schema — which is most of RouterOS scripting. The same + * refusal inside brackets is the same abstention (#572), so it gets the same + * code, spanned on the substitution. */ -function diagnosticsForStatement(s: ExplainStatement): ExplainDiagnostic[] { +function abstentionDiagnostics( + s: ExplainStatement | ExplainSubcommand, +): ExplainDiagnostic[] { const out: ExplainDiagnostic[] = []; if (s.resolution === "ambiguous") out.push({ @@ -2794,19 +2832,6 @@ function diagnosticsForStatement(s: ExplainStatement): ExplainDiagnostic[] { span: s.span, ev: s.ev, }); - // The #192 signal: a reading that is CORRECT while the document context was - // already lost. Info severity — the statement resolved without consuming the - // context, so nothing about it is wrong; a consumer ranking readings or - // offering completions is the one that needs to know. - if (s.resolution === "resolved" && !s.contextCertain) - out.push({ - code: "explain/canonicalizer/context-lost", - severity: "info", - message: - "resolved without the document's menu context, which an earlier unreadable statement destroyed", - span: s.span, - ev: s.ev, - }); return out; } diff --git a/test/unit/explain-corrected-anchors.test.ts b/test/unit/explain-corrected-anchors.test.ts index 6a890b5d..d32bdada 100644 --- a/test/unit/explain-corrected-anchors.test.ts +++ b/test/unit/explain-corrected-anchors.test.ts @@ -147,7 +147,7 @@ const SYNTAX = { "7.25": "validation/syntax", "7.23.8": "validation/syntax" }; // does not. const EXPECTED: Record< string, - { device: Record; offline: "pass" | "fail" } + { device: Record; offline: "pass" | "warn" | "fail" } > = { "apply-operator": { device: ACCEPTED, offline: "pass" }, "apply-operator-declared": { device: ACCEPTED, offline: "pass" }, @@ -186,7 +186,8 @@ const EXPECTED: Record< }, offline: "pass", }, - "control-unknown-command": { device: SYNTAX, offline: "pass" }, + // #572: the bracketed unknown command warns like its top-level form. + "control-unknown-command": { device: SYNTAX, offline: "warn" }, }; describe("corrected anchors against recorded device :parse (#374)", () => { diff --git a/test/unit/explain-structure.test.ts b/test/unit/explain-structure.test.ts index 42f95b3c..909296cb 100644 --- a/test/unit/explain-structure.test.ts +++ b/test/unit/explain-structure.test.ts @@ -307,3 +307,46 @@ test.skipIf(process.env["CENTRS_CHECK_CORPUS"] !== "1")( }, 120_000, ); + +// #572: a bracketed subcommand the verb reader cannot place gets the warning +// its top-level form gets, spanned on the substitution, never an error. +test.each([ + [ + ":local varID [nosuchcmd where name=x]", + "explain/canonicalizer/unresolved-statement", + { start: 13, end: 37 }, + ], + [ + ":put [nosuchcmd]", + "explain/canonicalizer/unresolved-statement", + { start: 5, end: 16 }, + ], + [ + ":put [/ip/address/nosuchverb]", + "explain/canonicalizer/ambiguous-statement", + { start: 5, end: 29 }, + ], +])( + "a bracketed abstention warns like its statement: %s", + (input, code, span) => { + const result = explainCommand(input); + expect(result.verdict).toBe("warn"); + expect( + result.diagnostics.map((d) => ({ + code: d.code, + severity: d.severity, + span: d.span, + })), + ).toEqual([{ code, severity: "warning", span }]); + }, +); + +test("a bracket with no command head stays silent (#572)", () => { + // `[$f]` and `[(…)]` are a call and an expression, not commands, and the + // device accepts them (corrected-anchors rows `trailing-selector*`). + for (const input of [":put [$f]", ":put [$f arg=1]", ":put [($a . $b)]"]) { + const result = explainCommand(input); + expect(result.structure.subcommands[0]?.resolution).toBe("unknown"); + expect(result.verdict).toBe("pass"); + } +}); From 1d068bfd011eb34872406a95efd11ed3d9658312 Mon Sep 17 00:00:00 2001 From: Amm0Bot Date: Thu, 26 Nov 2026 16:46:26 -0800 Subject: [PATCH 2/2] fix(explain): warn on command-shaped bracket refusals; read \$[ as text (#572) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review on #680: - A bracket Q3 refused but that still has a command head (a variable path segment, a relative command under a lost context) warns like its statement form. Only a bracket with no command in it ([$f], [(…)], a pasted prompt) stays silent. Corpus verdict moves are unchanged (14). - scanInterpolations skipped no escapes, so `:put "\$[x]"` produced a phantom substitution and now a warning. An escaped character is skipped. - Re-adopt the conformance fixture: 3 of 875 cases move pass -> warn. - Matrix nit for the non-ASCII bracket-span widening. Session: 4f3a1170-9bf0-4eac-97bd-7c9c9d0e6bbd Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 18 ++++++++++------- docs/MATRIX.md | 4 ++++ src/explain.ts | 19 +++++++++++++----- src/explain/pathresolve.ts | 6 ++++++ test/fixtures/explain/conformance.json | 6 +++--- test/unit/explain-structure.test.ts | 27 ++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7778de9a..116ce3eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,13 +101,17 @@ publish `0.2.0-next.` to `@next`; `@latest` stays on 0.1.10. - `explain` warns on an unknown or ambiguous command inside `[ … ]` (#572), the same `explain/canonicalizer/unresolved-statement` or `ambiguous-statement` warning its top-level form already got, spanned on - the substitution: `:put [/ip/address/nosuchverb]` was `pass`. It stays a - warning, never an error. A bracket with no command head, such as `[$f]` or - `[($a . $b)]`, stays silent, as before. On the pinned 948-script corpus, - 14 scripts move from `pass` to `warn`: 3 that every recorded RouterOS - version rejects, and 11 it accepts, which the offline reader cannot place - at the top level either (colon-less builtins like `[typeof $b]`, bare - no-argument commands like `[/terminal/inkey]`). + the substitution: `:put [/ip/address/nosuchverb]` and + `:put [/ip/$menu/x]` were `pass`. It stays a warning, never an error. A + bracket with no command in it, such as `[$f]` or `[($a . $b)]`, stays + silent, as before, and an escaped `\$[…]` inside a string is now read as + text rather than as a substitution. On the pinned 948-script corpus, 14 + scripts move from `pass` to `warn`: 3 that every recorded RouterOS version + rejects, and 11 it accepts, which the offline reader cannot place at the + top level either (colon-less builtins like `[typeof $b]`, bare no-argument + commands like `[/terminal/inkey]`). In the 875-case conformance set, 3 move: + `parameters-and-flags/r20` (rejected), `colon-prefix/p3` and `p12` + (accepted, colon-less builtins). - `execute --via mac-telnet` returns the command's output without its echo (#575). The reader skipped as many screen columns as the command was long, but the console's echo differs from the bytes sent: it drops a trailing diff --git a/docs/MATRIX.md b/docs/MATRIX.md index 7cc3b2e0..1ffca9bb 100644 --- a/docs/MATRIX.md +++ b/docs/MATRIX.md @@ -495,6 +495,10 @@ done or declined. A nit that needs more explanation is an issue. - `explain` offline: `/system/script/run staticUrl` reads as `ambiguous` "bare path, nothing follows", so a positional operand after `run` is taken for a path segment (seen in the #572 corpus census). +- `explain` offline: inside a non-ASCII statement (or one behind a BOM), + bracket spans widen to the whole statement (`pathresolve.ts` `Loc` + fallback), so two `[…]` warnings there are identical; mapping them through + `coordinates.runs` as #523 did for blocks would keep each bracket distinct. - `.github/instructions/`: trim Copilot-era verbosity when the TIKOCI-wide files are collected in `tikoci.github.io`. - Error fields: `docs/CONSTITUTION.md` → Error model specifies `message` and diff --git a/src/explain.ts b/src/explain.ts index 1a1439c9..0a26e219 100644 --- a/src/explain.ts +++ b/src/explain.ts @@ -1611,11 +1611,12 @@ export function explainCommand( // segments and pairing the two by index attaches the wrong span to every statement after // the first block. Each split carries its own document-space span. // - // The verb reader warns on a bracket it read (#572). A bracket it never - // read — a `[$f]` call or a `[(…)]` expression has no command head — stays - // silent: the device accepts those (fixture rows `trailing-selector*`). + // A bracketed command that cannot be placed warns like its statement form + // (#572). A bracket with no command in it — a `[$f]` call, a `[(…)]` + // expression, a pasted prompt — stays silent: the device accepts those + // (fixture rows `trailing-selector*`). const bracketReadings = brackets.resolutions.map((r) => ({ - read: !bracketRefused(r), + read: !bracketHasNoCommand(r), subcommand: subcommandOf(r), })); const subcommands = bracketReadings.map(({ subcommand }) => subcommand); @@ -2718,11 +2719,19 @@ function enforceGateParity( return out; } -/** Q3 refused the bracket: it found no command head to read. */ +/** Q3 refused the bracket, so the verb reader never sees it. */ function bracketRefused(r: Resolution): boolean { return r.unresolved !== undefined || r.path === null; } +/** + * Q3 found no command head: a `[$f]` call, a `[(…)]` expression or a pasted + * prompt. A variable path segment or a lost context still has one. + */ +function bracketHasNoCommand(r: Resolution): boolean { + return r.path === null && r.tokens.length === 0; +} + /** One `[…]` substitution in the envelope's vocabulary. See {@link ExplainSubcommand}. */ function subcommandOf(r: Resolution): ExplainSubcommand { // Q3 refused: the bracket is unreadable and Q6's reading of the same text diff --git a/src/explain/pathresolve.ts b/src/explain/pathresolve.ts index 78eda0f1..64121424 100644 --- a/src/explain/pathresolve.ts +++ b/src/explain/pathresolve.ts @@ -1429,6 +1429,12 @@ function scanInterpolations( ): void { const { text, start: lo, end: hi } = range; for (let i = lo; i < hi - 1; i++) { + // `\$[` is literal text: the escape spends the `$` (CHR, the execute + // canonicalizer contract), so skip the escaped character whatever it is. + if (text[i] === "\\") { + i++; + continue; + } if (text[i] !== "$" || text[i + 1] !== "[") continue; const scanned = scanStringInterpolation(text, i + 1, hi); const end = scanned.end; diff --git a/test/fixtures/explain/conformance.json b/test/fixtures/explain/conformance.json index fa1ec0d1..0a0c65dc 100644 --- a/test/fixtures/explain/conformance.json +++ b/test/fixtures/explain/conformance.json @@ -32,8 +32,8 @@ } }, "offline": { - "pass": 607, - "warn": 78, + "pass": 604, + "warn": 81, "fail": 190 }, "device": { @@ -138,7 +138,7 @@ "wiresage-conformance/parameters-and-flags/r11.rsc": "pass; device embedded-diagnostic", "wiresage-conformance/parameters-and-flags/r12.rsc": "pass; device embedded-diagnostic", "wiresage-conformance/parameters-and-flags/r2.rsc": "pass; device embedded-diagnostic", - "wiresage-conformance/parameters-and-flags/r20.rsc": "pass; device embedded-diagnostic", + "wiresage-conformance/parameters-and-flags/r20.rsc": "warn; device embedded-diagnostic", "wiresage-conformance/parameters-and-flags/r22.rsc": "pass; device embedded-diagnostic", "wiresage-conformance/parameters-and-flags/r23.rsc": "pass; device embedded-diagnostic", "wiresage-conformance/parameters-and-flags/r3.rsc": "pass; device embedded-diagnostic", diff --git a/test/unit/explain-structure.test.ts b/test/unit/explain-structure.test.ts index 909296cb..0fd502ba 100644 --- a/test/unit/explain-structure.test.ts +++ b/test/unit/explain-structure.test.ts @@ -326,6 +326,16 @@ test.each([ "explain/canonicalizer/ambiguous-statement", { start: 5, end: 29 }, ], + [ + ":put [/ip/$menu/nosuchcmd]", + "explain/canonicalizer/unresolved-statement", + { start: 5, end: 26 }, + ], + [ + ':put "$[nosuchcmd]"', + "explain/canonicalizer/unresolved-statement", + { start: 6, end: 18 }, + ], ])( "a bracketed abstention warns like its statement: %s", (input, code, span) => { @@ -341,6 +351,23 @@ test.each([ }, ); +test("a bracket under a variable path warns on itself and its child (#572)", () => { + // Q3 refuses the outer bracket (variable path segment) and so loses the + // inner one's menu context; both are commands, so both abstain out loud. + const result = explainCommand(":put [/interface/$type/get [nosuchcmd]]"); + expect(result.verdict).toBe("warn"); + expect(result.diagnostics.map((d) => [d.code, d.span])).toEqual([ + ["explain/canonicalizer/unresolved-statement", { start: 5, end: 39 }], + ["explain/canonicalizer/unresolved-statement", { start: 27, end: 38 }], + ]); +}); + +test("an escaped `\\$[` in a string is text, not a substitution (#572)", () => { + const result = explainCommand(':put "\\$[nosuchcmd]"'); + expect(result.structure.subcommands).toEqual([]); + expect(result.verdict).toBe("pass"); +}); + test("a bracket with no command head stays silent (#572)", () => { // `[$f]` and `[(…)]` are a call and an expression, not commands, and the // device accepts them (corrected-anchors rows `trailing-selector*`).