feat(signals): decode recordings with bounded sigrok output (#2339)

Decode caller-selected protocols from existing recordings through shared
CLI/MCP Evidence. Page literal output lines after complete decoding;
preserve explicit full output, source identity and failed cleanup ownership.

Verified real sigrok-cli 0.7.2 workflows, published UART/SPI/I2C recordings,
independent UART values, selector limits, SDK schemas and cleanup regressions.

Co-authored-by: Olorinm <59321404+Olorinm@users.noreply.github.com>
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
This commit is contained in:
Ysopwnable
2026-10-12 20:59:02 +00:00
committed by GitHub
co-authored by Olorinm
parent 5f79c852af
commit 91b8f587cd
50 changed files with 2715 additions and 252 deletions
@@ -0,0 +1,48 @@
import { expect, it } from "vitest";
import { windowAnnotationText } from "./annotationTextWindow.js";
it("preserves line terminators, blank lines and literal display text", () => {
const text = "1-2 uart-1: 界\r\n\ntext without a sample range\nlast";
expect(windowAnnotationText(text, 1, 2)).toEqual({
annotations_text: "\ntext without a sample range\n",
output_window: { total_lines: 4, returned_lines: 2, has_more: true },
});
expect(windowAnnotationText(text, 0, null).annotations_text).toBe(text);
expect(windowAnnotationText(text, 3, 2)).toEqual({
annotations_text: "last",
output_window: { total_lines: 4, returned_lines: 1, has_more: false },
});
});
it.each(["", "\n", "line\n", "line"])(
"does not invent a trailing line or normalize %j",
(text) => {
expect(windowAnnotationText(text, 0, null)).toEqual({
annotations_text: text,
output_window: {
total_lines: text === "" ? 0 : 1,
returned_lines: text === "" ? 0 : 1,
has_more: false,
},
});
},
);
it("returns an empty covered window beyond the last line without unsafe arithmetic", () => {
expect(
windowAnnotationText(
"one\ntwo",
Number.MAX_SAFE_INTEGER,
Number.MAX_SAFE_INTEGER,
),
).toEqual({
annotations_text: "",
output_window: { total_lines: 2, returned_lines: 0, has_more: false },
});
});
it("does not replace source UTF-16 code units when copying a partial window", () => {
expect(
windowAnnotationText("first\n\ud800\nlast", 1, 1).annotations_text,
).toBe("\ud800\n");
});
@@ -0,0 +1,35 @@
/** Page literal LF-separated display lines without interpreting annotation semantics. */
export const windowAnnotationText = (
text: string,
offset: number,
limit: number | null,
) => {
let total = 0;
let returned = 0;
let start = text.length;
let end = text.length;
for (let from = 0; from < text.length;) {
const newline = text.indexOf("\n", from);
const next = newline < 0 ? text.length : newline + 1;
if (total === offset) start = from;
if (total >= offset && (limit === null || returned < limit)) {
end = next;
returned++;
}
total++;
from = next;
}
return {
// Copy partial windows so retained Evidence does not retain the full producer
// string. UTF-16 preserves every source code unit, including lone surrogates.
annotations_text:
start === 0 && end === text.length
? text
: Buffer.from(text.slice(start, end), "utf16le").toString("utf16le"),
output_window: {
total_lines: total,
returned_lines: returned,
has_more: Math.min(offset, total) + returned < total,
},
};
};
+148
View File
@@ -0,0 +1,148 @@
import { z } from "zod";
import { localPathStringSchema } from "../localPath.js";
import { windowAnnotationText } from "./annotationTextWindow.js";
// These strings are single sigrok arguments, never shell fragments.
const selection = z
.string()
.min(1)
.max(8192)
.refine((value) => !value.includes("\0"), "NUL is not an argument character");
const signalInputShape = z.strictObject({
path: localPathStringSchema.describe("Absolute local recording path"),
decoders: selection.describe(
"sigrok decoder stack, options and channel assignments, e.g. uart:rx=RX:baudrate=9600",
),
input_format: selection
.nullable()
.default(null)
.describe(
"Explicit sigrok input format and options; omitted means autodetection",
),
annotations: selection
.nullable()
.default(null)
.describe("Optional annotation selection, e.g. uart=rx-data"),
line_offset: z
.number()
.int()
.nonnegative()
.default(0)
.describe("Skip this many literal LF-separated output lines; default 0"),
line_limit: z
.number()
.int()
.positive()
.nullable()
.default(50)
.describe(
"Return at most this many output lines; default 50. Null explicitly returns all remaining lines within producer limits. Lines are not annotation counts.",
),
});
export const withinSignalRecordingSelectorBudget = (input: {
decoders: string;
input_format: string | null;
annotations: string | null;
}): boolean => {
let bytes = 0;
for (const [value, prefix] of [
[input.decoders, "--protocol-decoders="],
[input.input_format, "--input-format="],
[input.annotations, "--protocol-decoder-annotations="],
] as const)
if (value !== null)
bytes +=
Buffer.byteLength(prefix, "utf8") +
Buffer.byteLength(value, "utf8") +
9;
return bytes <= 32768;
};
export const signalRecordingSelectorBudgetIssue = {
message:
"Signal selectors exceed the 32 KiB encoded argument budget; reduce decoder, format or annotation selections.",
};
export const decodeSignalRecordingInputSchema = signalInputShape.refine(
withinSignalRecordingSelectorBudget,
signalRecordingSelectorBudgetIssue,
);
export const signalRecordingOptionsSchema = signalInputShape
.omit({ path: true })
.refine(
withinSignalRecordingSelectorBudget,
signalRecordingSelectorBudgetIssue,
);
export type DecodeSignalRecordingInput = z.infer<
typeof decodeSignalRecordingInputSchema
>;
export const signalRecordingSchema = z
.strictObject({
artifact: z.strictObject({
path: z.string(),
sha256: z.string().regex(/^[a-f0-9]{64}$/),
bytes: z.number().int().nonnegative(),
}),
selection: decodeSignalRecordingInputSchema,
annotations_text: z
.string()
.describe(
"Selected literal lines of sigrok display output with unchanged sample ranges and line endings; not a lossless binary protocol representation",
),
output_window: z.strictObject({
total_lines: z
.number()
.int()
.nonnegative()
.describe(
"Literal LF-separated lines in completed producer output, not annotation or sample counts",
),
returned_lines: z.number().int().nonnegative(),
has_more: z.boolean(),
}),
diagnostics: z.string(),
tool: z.strictObject({
configured_path: z.string(),
path: z.string(),
sha256: z.string().regex(/^[a-f0-9]{64}$/),
version_banner: z.string(),
version_diagnostics: z.string(),
arguments: z.array(z.string()),
}),
resource_limiter: z.strictObject({
configured_path: z.string(),
path: z.string(),
sha256: z.string().regex(/^[a-f0-9]{64}$/),
arguments: z.array(z.string()),
}),
limits: z.strictObject({
input_bytes: z.number().int().positive(),
output_bytes: z.number().int().positive(),
command_timeout_ms: z.number().int().positive(),
address_space_bytes: z.number().int().nonnegative(),
file_bytes: z.number().int().nonnegative(),
cpu_seconds: z.number().int().nonnegative(),
}),
hardware_acquisition: z.literal("not-performed"),
})
.superRefine((report, context) => {
const remaining = Math.max(
report.output_window.total_lines - report.selection.line_offset,
0,
);
const expected =
report.selection.line_limit === null
? remaining
: Math.min(remaining, report.selection.line_limit);
if (
report.output_window.returned_lines !== expected ||
windowAnnotationText(report.annotations_text, 0, null).output_window
.total_lines !== expected ||
report.output_window.has_more !== remaining > expected
)
context.addIssue({
code: "custom",
path: ["output_window"],
message:
"Output text and window must agree with examined lines and selected offset/limit",
});
});