Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ publish `0.2.0-next.<run_number>` 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]` 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
Expand Down
1 change: 1 addition & 0 deletions GLOSSARY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions docs/MATRIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,18 @@ 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+<cols>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).
- `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
Expand Down
94 changes: 64 additions & 30 deletions src/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,15 @@ 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.
//
// 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: !bracketHasNoCommand(r),
subcommand: subcommandOf(r),
}));
const subcommands = bracketReadings.map(({ subcommand }) => subcommand);
const readStatements: ExplainStatement[] = verbs.splits.map((split, index) =>
statementOf(
split,
Expand Down Expand Up @@ -1699,6 +1708,9 @@ export function explainCommand(
pairs.filter((pair) => pair.valueShaped).map(spacedArgumentDiagnostic),
),
...statements.flatMap((s) => diagnosticsForStatement(s)),
...bracketReadings.flatMap(({ read, subcommand }) =>
read ? abstentionDiagnostics(subcommand) : [],

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Preserve a distinct source span for each bracket warning. :put ("é" . [bad1] . [bad2]) emits two otherwise identical unresolved-statement diagnostics with [0,29) for both; the two substitutions occupy [13,19) and [22,28). A same-line BOM also widens the bracket warning. The existing fallback is documented in the matrix, but the new warnings inherit it and #572 explicitly requires a span on the subcommand. Map these warnings to the bracket offsets and test the byte slices, or explicitly carry the span work into a linked follow-up and qualify the changelog and PR claim.

),
].sort(compareExplainDiagnostics);

const valueFacts = valuesOf(
Expand Down Expand Up @@ -1800,7 +1812,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,
Expand Down Expand Up @@ -2707,23 +2719,35 @@ function enforceGateParity(
return out;
}

/** Q3 refused the bracket, so the verb reader never sees it. */
function bracketRefused(r: Resolution): boolean {
return r.unresolved !== undefined || r.path === null;
Comment thread
mobileskyfi marked this conversation as resolved.
Resolved
}

/**
* 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
// 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 },
Comment thread
mobileskyfi marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -2765,16 +2789,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({
Expand All @@ -2794,19 +2841,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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/explain/pathresolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/explain/conformance.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
}
},
"offline": {
"pass": 607,
"warn": 78,
"pass": 604,
"warn": 81,
"fail": 190
},
"device": {
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions test/unit/explain-corrected-anchors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const SYNTAX = { "7.25": "validation/syntax", "7.23.8": "validation/syntax" };
// does not.
const EXPECTED: Record<
string,
{ device: Record<string, string>; offline: "pass" | "fail" }
{ device: Record<string, string>; offline: "pass" | "warn" | "fail" }
> = {
"apply-operator": { device: ACCEPTED, offline: "pass" },
"apply-operator-declared": { device: ACCEPTED, offline: "pass" },
Expand Down Expand Up @@ -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" },
Comment thread
mobileskyfi marked this conversation as resolved.
Resolved
};

describe("corrected anchors against recorded device :parse (#374)", () => {
Expand Down
70 changes: 70 additions & 0 deletions test/unit/explain-structure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,73 @@ 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 },
],
[
":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) => {
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 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]"');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 — Pin the even-backslash case mentioned in the earlier review reply. This test covers one literal backslash before $[ and the table covers zero. I confirmed two literal backslashes before $[ still produce an active substitution. Add that case so a future scanner that checks only the immediately preceding character would fail the test.

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*`).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cited trailing-selector* rows ground [$f] as part of a selector command. They do not contain [$f arg=1], [($a . $b)], or pasted prompts. Narrow the fixture attribution here and in the PR body to the control it actually covers.

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");
}
});
Loading