feat(signals): discover installed decoders and option help (#2487)
* feat(signals): discover installed decoders and selected option help * fix(signals): bound retained discovery data and preserve resource failures * fix(signals): complete CLI help and portable decoder patterns * test(signals): verify case-insensitive decoder help queries * fix(signals): retain discovery operation across cleanup retries
This commit is contained in:
@@ -30,6 +30,9 @@ it.each([
|
||||
silentLogger,
|
||||
{},
|
||||
{
|
||||
inspectDecoders: async () => {
|
||||
throw new Error("Unexpected discovery call");
|
||||
},
|
||||
decode: async () => {
|
||||
called = true;
|
||||
throw new Error("Must not decode");
|
||||
@@ -84,6 +87,9 @@ it.each([false, true])(
|
||||
silentLogger,
|
||||
{},
|
||||
{
|
||||
inspectDecoders: async () => {
|
||||
throw new Error("Unexpected discovery call");
|
||||
},
|
||||
decode: async () => err(original),
|
||||
close: async () => {
|
||||
await Promise.resolve();
|
||||
|
||||
@@ -8,41 +8,51 @@ import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
|
||||
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
||||
|
||||
it("advertises valid target-free signal decoding schemas and actionable missing-tool diagnostics", async () => {
|
||||
const harness = await createApplicationMcpHarness({
|
||||
serverOptions: { environment: {} },
|
||||
});
|
||||
onTestFinished(harness.close);
|
||||
const tool = (await harness.client.listTools()).tools.find(
|
||||
({ name }) => name === "decode_signal_recording",
|
||||
);
|
||||
if (tool?.outputSchema === undefined) throw new Error("Signal tool missing");
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
const inputSchema: Record<string, unknown> = tool.inputSchema;
|
||||
const outputSchema: Record<string, unknown> = tool.outputSchema;
|
||||
expect(ajv.validateSchema(inputSchema)).toBe(true);
|
||||
expect(ajv.validateSchema(outputSchema)).toBe(true);
|
||||
const input = {
|
||||
path: process.platform === "win32" ? "C:\\recording.vcd" : "/recording.vcd",
|
||||
decoders: "uart",
|
||||
};
|
||||
expect(ajv.validate(inputSchema, input)).toBe(true);
|
||||
expect(ajv.validate(inputSchema, { ...input, driver: "demo" })).toBe(false);
|
||||
const result = await harness.client.callTool({
|
||||
name: tool.name,
|
||||
arguments: input,
|
||||
});
|
||||
expect(parseMcpToolError(result)).toMatchObject({
|
||||
error: {
|
||||
code: "capability_unavailable",
|
||||
details: {
|
||||
operation: tool.name,
|
||||
reason: expect.stringContaining("REA_SIGROK_COMMAND"),
|
||||
it.each(["decode_signal_recording", "inspect_signal_decoders"])(
|
||||
"advertises valid target-free schemas and missing-tool diagnostics: %s",
|
||||
async (name) => {
|
||||
const harness = await createApplicationMcpHarness({
|
||||
serverOptions: { environment: {} },
|
||||
});
|
||||
onTestFinished(harness.close);
|
||||
const tool = (await harness.client.listTools()).tools.find(
|
||||
(tool) => tool.name === name,
|
||||
);
|
||||
if (tool?.outputSchema === undefined)
|
||||
throw new Error("Signal tool missing");
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
const inputSchema: Record<string, unknown> = tool.inputSchema;
|
||||
const outputSchema: Record<string, unknown> = tool.outputSchema;
|
||||
expect(ajv.validateSchema(inputSchema)).toBe(true);
|
||||
expect(ajv.validateSchema(outputSchema)).toBe(true);
|
||||
const input =
|
||||
name === "inspect_signal_decoders"
|
||||
? { query: "uart" }
|
||||
: {
|
||||
path:
|
||||
process.platform === "win32"
|
||||
? "C:\\recording.vcd"
|
||||
: "/recording.vcd",
|
||||
decoders: "uart",
|
||||
};
|
||||
expect(ajv.validate(inputSchema, input)).toBe(true);
|
||||
expect(ajv.validate(inputSchema, { ...input, driver: "demo" })).toBe(false);
|
||||
const result = await harness.client.callTool({
|
||||
name: tool.name,
|
||||
arguments: input,
|
||||
});
|
||||
expect(parseMcpToolError(result)).toMatchObject({
|
||||
error: {
|
||||
code: "capability_unavailable",
|
||||
details: {
|
||||
operation: tool.name,
|
||||
reason: expect.stringContaining("REA_SIGROK_COMMAND"),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
await harness.client.ping();
|
||||
});
|
||||
});
|
||||
await harness.client.ping();
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["cancelled", "cleanup"])(
|
||||
"retains completed Evidence after %s failure",
|
||||
@@ -77,6 +87,9 @@ it.each(["cancelled", "cleanup"])(
|
||||
},
|
||||
);
|
||||
const signalRecording = new SignalRecordingService({
|
||||
inspectDecoders: async () => {
|
||||
throw new Error("Unexpected discovery call");
|
||||
},
|
||||
decode: async () => err(failure),
|
||||
close: async () => {},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { inspectSignalDecodersInputSchema } from "../../../src/domain/signals/signalDecoders.js";
|
||||
import { parseDecoderDiscovery } from "../../../src/signals/parseDecoderDiscovery.js";
|
||||
|
||||
it("rejects malformed catalogs and the wrong decoder identity", () => {
|
||||
for (const text of [
|
||||
"",
|
||||
"Supported protocol decoders:\n",
|
||||
"Supported protocol decoders:\n uart UART\n uart Duplicate",
|
||||
"Supported protocol decoders:\n malformed",
|
||||
]) {
|
||||
expect(() =>
|
||||
parseDecoderDiscovery(text, inspectSignalDecodersInputSchema.parse({})),
|
||||
).toThrow();
|
||||
}
|
||||
expect(() =>
|
||||
parseDecoderDiscovery(
|
||||
"ID: spi\nOptions:",
|
||||
inspectSignalDecodersInputSchema.parse({ decoder: "uart" }),
|
||||
),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("keeps help line ordinals, blank lines and literal special characters through selection", () => {
|
||||
const text = "ID: uart\r\nOptions:\r\n\r\n- mode: A[0] (default 'x')\r\n";
|
||||
const result = parseDecoderDiscovery(
|
||||
text,
|
||||
inspectSignalDecodersInputSchema.parse({
|
||||
decoder: "uart",
|
||||
query: "[0]",
|
||||
offset: 0,
|
||||
limit: 1,
|
||||
}),
|
||||
);
|
||||
expect(result.help_lines).toEqual([
|
||||
{ line_number: 4, text: "- mode: A[0] (default 'x')" },
|
||||
]);
|
||||
expect(result.output_window.examined).toBe(4);
|
||||
});
|
||||
|
||||
it("bounds line expansion before constructing help records", () => {
|
||||
expect(() =>
|
||||
parseDecoderDiscovery(
|
||||
"ID: uart\n" + "\n".repeat(65536),
|
||||
inspectSignalDecodersInputSchema.parse({ decoder: "uart" }),
|
||||
),
|
||||
).toThrow("representation budget");
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { signalDecodersSchema } from "../../../src/domain/signals/signalDecoders.js";
|
||||
import { OwnedCommandFailure } from "../../../src/process/OwnedCommand.js";
|
||||
import { it, expect, onTestFinished } from "vitest";
|
||||
import { writeFile, readdir, chmod, symlink } from "node:fs/promises";
|
||||
@@ -23,13 +24,17 @@ const fixture = async (mode = "ok", failCleanup = false) => {
|
||||
await writeFile(
|
||||
script,
|
||||
`import {readFileSync, writeFileSync, chmodSync, truncateSync} from 'node:fs';
|
||||
if (process.argv.includes('--version')) { console.log('sigrok-cli 0.7.2\\nlibsigrokdecode 0.5.3'); console.error('version warning'); }
|
||||
if (process.argv.includes('--version')) { console.log(${JSON.stringify(mode)} === 'bad-version' ? 'sigrok-cli 0.7.1' : 'sigrok-cli 0.7.2\\nlibsigrokdecode 0.5.3'); console.error('version warning'); }
|
||||
else if (${JSON.stringify(mode)} === 'bad-version') console.log('unexpected version fallback');
|
||||
else if (${JSON.stringify(mode)} === 'nonzero') { console.error('invalid decoder'); process.exitCode = 1; }
|
||||
else if (${JSON.stringify(mode)} === 'malformed-copy') { chmodSync('recording', 0o600); writeFileSync('recording', 'changed'); }
|
||||
else if (${JSON.stringify(mode)} === 'oversized-copy') { chmodSync('recording', 0o600); truncateSync('recording', 32 * 1024 * 1024 + 1); }
|
||||
else if (${JSON.stringify(mode)} === 'line-budget') process.stdout.write('ID: uart' + '\\n'.repeat(65536));
|
||||
else if (${JSON.stringify(mode)} === 'overflow') process.stdout.write('x'.repeat(5 * 1024 * 1024));
|
||||
else if (${JSON.stringify(mode)} === 'wait') { console.log('ready'); setInterval(() => {}, 1000); }
|
||||
else if (${JSON.stringify(mode)} === 'many') process.stdout.write(Array.from({length:90},(_,i)=>'line '+(i+1)+'\\r\\n').join(''));
|
||||
else if (process.argv.includes('--list-supported')) { console.log('Supported hardware drivers:\\n ignored Device\\nSupported protocol decoders:\\n uart Universal Asynchronous Receiver/Transmitter\\n i2c Inter-Integrated Circuit\\n'); console.error('catalog warning'); }
|
||||
else if (process.argv.includes('--show')) console.log('ID: uart\\nOptions:\\n- baudrate: Baud rate (default 115200)\\n- parity: Parity');
|
||||
else { if(readFileSync('recording', 'utf8') !== 'recording bytes') throw Error('wrong copy'); console.log('10-20 uart-1: 42'); console.error('producer warning'); }
|
||||
`,
|
||||
);
|
||||
@@ -285,6 +290,9 @@ it("retains a completed observation when cancellation arrives at the service bou
|
||||
const execution = await f.service.provider.decode(input);
|
||||
if (!execution.ok) throw execution.error;
|
||||
const service = new SignalRecordingService({
|
||||
inspectDecoders: async () => {
|
||||
throw new Error("Unexpected discovery call");
|
||||
},
|
||||
decode: async () => {
|
||||
f.controller.abort();
|
||||
return execution;
|
||||
@@ -419,3 +427,182 @@ it("preserves termination signal and effective file ceiling as a resource failur
|
||||
capturedOutput: { signal: "SIGXFSZ", stderr: "before file limit\n" },
|
||||
});
|
||||
});
|
||||
|
||||
it("discovers decoders without reading a recording and retains installation provenance", async () => {
|
||||
const f = await fixture();
|
||||
for (const [input, expected] of [
|
||||
[
|
||||
{ query: "UART" },
|
||||
{
|
||||
decoders: [
|
||||
{ id: "uart", name: "Universal Asynchronous Receiver/Transmitter" },
|
||||
],
|
||||
output_window: {
|
||||
examined: 2,
|
||||
matching: 1,
|
||||
returned: 1,
|
||||
has_more: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{ limit: 1 },
|
||||
{
|
||||
decoders: [
|
||||
{ id: "uart", name: "Universal Asynchronous Receiver/Transmitter" },
|
||||
],
|
||||
output_window: { matching: 2, returned: 1, has_more: true },
|
||||
},
|
||||
],
|
||||
[
|
||||
{ offset: 1, limit: null },
|
||||
{
|
||||
decoders: [{ id: "i2c", name: "Inter-Integrated Circuit" }],
|
||||
output_window: { returned: 1, has_more: false },
|
||||
},
|
||||
],
|
||||
[
|
||||
{ decoder: "uart", query: "baud" },
|
||||
{
|
||||
help_lines: [
|
||||
{ line_number: 3, text: "- baudrate: Baud rate (default 115200)" },
|
||||
],
|
||||
output_window: { unit: "help-lines", examined: 4, matching: 1 },
|
||||
},
|
||||
],
|
||||
[
|
||||
{ query: "no-such-decoder" },
|
||||
{
|
||||
decoders: [],
|
||||
output_window: { examined: 2, matching: 0, returned: 0 },
|
||||
},
|
||||
],
|
||||
] as const) {
|
||||
const result = await f.service.inspectDecoders(input);
|
||||
if (!result.ok) throw result.error;
|
||||
const report = signalDecodersSchema.parse(result.value.normalized_result);
|
||||
expect(report).toMatchObject(expected);
|
||||
expect(result.value.operation).toBe("inspect_signal_decoders");
|
||||
expect(report.tool.version_diagnostics).toBe("version warning\n");
|
||||
expect(report.tool.arguments).toContain("--dont-scan");
|
||||
expect(
|
||||
report.tool.arguments.some((arg) => arg.startsWith("--input-file")),
|
||||
).toBe(false);
|
||||
}
|
||||
expect(
|
||||
(await readdir(f.directory)).filter((name) => name.startsWith("decoder-")),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("attributes discovery admission failures to discovery while sharing the decoding slot", async () => {
|
||||
const f = await fixture("gated");
|
||||
const active = f.service.decode({ path: f.path, decoders: "uart" });
|
||||
await f.acquired;
|
||||
const busy = await f.service.inspectDecoders({});
|
||||
f.release();
|
||||
if (!(await active).ok) throw new Error("Decode failed");
|
||||
expect(busy).toMatchObject({
|
||||
ok: false,
|
||||
error: { operation: "inspect_signal_decoders", resource: "capacity" },
|
||||
});
|
||||
await f.service.close();
|
||||
expect(await f.service.inspectDecoders({})).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
operation: "inspect_signal_decoders",
|
||||
_tag: "AnalysisCancelledError",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
"bad-version",
|
||||
"nonzero",
|
||||
"overflow",
|
||||
"line-budget",
|
||||
"wait",
|
||||
"oversized-tool",
|
||||
])("preserves discovery failure diagnostics: %s", async (mode) => {
|
||||
const f = await fixture(mode);
|
||||
const result = await f.service.inspectDecoders(
|
||||
{},
|
||||
{ signal: f.controller.signal },
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) throw new Error("Expected failure");
|
||||
expect(result.error).toMatchObject({
|
||||
operation: "inspect_signal_decoders",
|
||||
});
|
||||
if (mode === "line-budget") {
|
||||
expect(result.error).toMatchObject({
|
||||
_tag: "AnalysisResourceConstraintError",
|
||||
});
|
||||
expect(result.error.capturedOutput?.stdout).toBe(
|
||||
"ID: uart" + "\n".repeat(65536),
|
||||
);
|
||||
}
|
||||
if (mode === "bad-version")
|
||||
expect(result.error.capturedOutput).toMatchObject({
|
||||
stdout: "sigrok-cli 0.7.1\n",
|
||||
stderr: "version warning\n",
|
||||
});
|
||||
});
|
||||
|
||||
it("retains completed discovery when cleanup fails, then retries before accepting work", async () => {
|
||||
const f = await fixture("ok", true);
|
||||
const first = await f.service.inspectDecoders({ query: "uart" });
|
||||
expect(first).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
operation: "inspect_signal_decoders",
|
||||
partialObservation: { operation: "inspect_signal_decoders" },
|
||||
},
|
||||
});
|
||||
await f.service.inspectDecoders({});
|
||||
await f.service.decode({ path: f.path, decoders: "uart" });
|
||||
expect(f.calls()).toBe(2);
|
||||
for (let attempt = 0; attempt < 2; attempt++)
|
||||
await expect(f.service.close()).rejects.toMatchObject({
|
||||
operation: "inspect_signal_decoders",
|
||||
cleanupIncomplete: true,
|
||||
});
|
||||
f.allowCleanup();
|
||||
await f.service.close();
|
||||
});
|
||||
|
||||
it("rejects ambiguous decoder selectors before launch and retains malformed help output", async () => {
|
||||
const f = await fixture();
|
||||
for (const decoder of ["uart:baudrate=1", "uart,spi", "uart\0"]) {
|
||||
expect(await f.service.inspectDecoders({ decoder })).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisInputError" },
|
||||
});
|
||||
}
|
||||
expect(f.calls()).toBe(0);
|
||||
expect(await f.service.inspectDecoders({ decoder: "spi" })).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "AnalysisOutputError",
|
||||
capturedOutput: { stdout: expect.stringContaining("ID: uart") },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("retains completed discovery on cancellation during identity verification", async () => {
|
||||
const f = await fixture("post-run-abort");
|
||||
expect(
|
||||
await f.service.inspectDecoders(
|
||||
{ query: "uart" },
|
||||
{ signal: f.controller.signal },
|
||||
),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "AnalysisCancelledError",
|
||||
partialObservation: {
|
||||
operation: "inspect_signal_decoders",
|
||||
normalized_result: { decoders: [{ id: "uart" }] },
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user