Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(cli): merge sibling output filter slices
morlutoa4bf6f83a624
diff --git a/docs/cli.md b/docs/cli.mdindex 8a45627e9..6974ca6fe 100644--- a/docs/cli.md+++ b/docs/cli.md@@ -553,6 +553,13 @@ retain their complete typed diagnostic projection even when the selector matchesa field such as `code` or `message`, or names a missing field. `--full-output`controls whether the outer `{ok, error, meta}` envelope is included.+Sibling array slices that select different fields are combined by original row+index and emitted in source order, regardless of selector order. Selecting a+whole parent row keeps that row intact and takes precedence over narrower fields+selected from the same row. For example, `rows[0,2].name,rows[1,3].version`+combines both fields for overlapping rows, while `rows[0,2],rows[1,3].version`+retains rows 0 and 1 whole.+For `cutter-command`, `--json-output` requests command-specific JSON fromCutter when available. This is separate from `--format json`, which selects theCLI result representation; the global `--json` flag selects that same CLIdiff --git a/src/cli/cliResultOutput.ts b/src/cli/cliResultOutput.tsindex a7a7bf43e..921106b86 100644--- a/src/cli/cliResultOutput.ts+++ b/src/cli/cliResultOutput.ts@@ -217,6 +217,10 @@ export const createCliResultOutput = (? cliOperationFailurePayload(value): undefined;const failure = options.fullOutput ? operationFailure : undefined;+ const ownsFilteredOutput =+ operationFailure === undefined &&+ filterPaths !== undefined &&+ filterPaths.length > 0;// Incur owns suppressed human presentation, but a filter must never erase// the typed failure that this boundary already owns.if (@@ -226,30 +230,30 @@ export const createCliResultOutput = (!options.tokenCount)return false;- const filtered: unknown =+ const selected: unknown =operationFailure ??- (filterPaths === undefined ? value : Filter.apply(value, filterPaths));+ (filterPaths === undefined+ ? value+ : applyCliOutputFilter(value, filterPaths));+ const filtered =+ (format === "json" || format === "jsonl") &&+ ownsFilteredOutput &&+ Array.isArray(selected)+ ? normalizeJsonArrayValues(selected)+ : selected;const ownsSuccessfulTokenWindow =operationFailure === undefined &&options.tokenWindow &&!options.tokenCount &&format !== "json" &&format !== "jsonl";- if (- operationFailure === undefined &&- filterPaths !== undefined &&- filterPaths.length > 0 &&- (format === "json" || format === "jsonl") &&- Array.isArray(filtered)- )- normalizeFilteredJsonArrays(filtered);- // Leave successful non-JSON rendering to Incur. This owner handles typed- // failures when full output is requested or a data filter would erase the- // failure projection.+ // Delegate unfiltered successful non-JSON rendering to Incur. This boundary+ // owns filtered successes, requested failure projections, and token windows.if (format !== "json" &&format !== "jsonl" &&operationFailure === undefined &&+ !ownsFilteredOutput &&!ownsSuccessfulTokenWindow)return false;@@ -378,16 +382,18 @@ export const createCliResultOutput = (}// Once writing starts, a failed destination must not receive a second// document from Incur's error formatter.- const failureText =- operationFailure !== undefined && format !== "json" && format !== "jsonl"+ const formattedText =+ (operationFailure !== undefined || ownsFilteredOutput) &&+ format !== "json" &&+ format !== "jsonl"? Formatter.format(document, format): undefined;handled = true;try {- if (failureText !== undefined) {+ if (formattedText !== undefined) {// Incur's error sentinel only carries code/message/retryable. Render- // the full caller-visible failure at this output boundary.- await writeOutputText(destination, `${failureText}\n`);+ // the owned projection or caller-visible failure at this boundary.+ await writeOutputText(destination, `${formattedText}\n`);} else {await writeJsonOutput(error === undefined@@ -428,20 +434,124 @@ const fullFailureEnvelope = (};};-/** Filter.apply owns these mapped arrays; an empty path list returns the source. */-const normalizeFilteredJsonArrays = (selection: unknown[]): void => {- const pending = [selection];- while (pending.length > 0) {- const array = pending.pop()!;- for (let index = 0; index < array.length; index += 1) {- const value: unknown = array[index];- if (- value === undefined ||- typeof value === "function" ||- typeof value === "symbol"- )- array[index] = null;- else if (Array.isArray(value)) pending.push(value);+type FilterPaths = ReturnType<typeof Filter.parse>;+type FilterPath = FilterPaths[number];++/** Apply Incur's parsed path grammar while merging sibling array selections. */+const applyCliOutputFilter = (value: unknown, paths: FilterPaths): unknown => {+ if (paths.length === 0) return value;+ if (Array.isArray(value))+ return value.map((item) => applyCliOutputFilter(item, paths));+ if (+ paths.length === 1 &&+ paths[0]?.length === 1 &&+ paths[0][0] !== undefined &&+ "key" in paths[0][0]+ ) {+ const key = paths[0][0].key;+ if (typeof value !== "object" || value === null) return undefined;+ const selected = (value as Record<string, unknown>)[key];+ return typeof selected !== "object" || selected === null+ ? selected+ : { [key]: selected };+ }+ return projectObjectPaths(value, paths);+};++/** Build a fresh object projection; a path ending at a key selects its subtree. */+const projectObjectPaths = (value: unknown, paths: FilterPaths): unknown => {+ if (typeof value !== "object" || value === null) return {};+ const byKey = new Map<string, FilterPath[]>();+ for (const path of paths) {+ const [segment, ...tail] = path;+ if (segment === undefined || !("key" in segment)) continue;+ const siblings = byKey.get(segment.key) ?? [];+ siblings.push(tail);+ byKey.set(segment.key, siblings);+ }++ const result: Record<string, unknown> = {};+ for (const key of [...byKey.keys()].sort()) {+ const siblings = byKey.get(key)!;+ const child = (value as Record<string, unknown>)[key];+ if (child === undefined) continue;+ // Selecting a parent retains its complete original subtree and dominates+ // any narrower selections below it, independent of path order.+ if (siblings.some((path) => path.length === 0)) {+ result[key] = child;+ continue;+ }+ const slicePaths = siblings.filter(+ (path) => path[0] !== undefined && "start" in path[0],+ );+ if (slicePaths.length > 0 && Array.isArray(child)) {+ result[key] = projectArraySlices(child, slicePaths);+ continue;+ }+ const objectPaths = siblings.filter(+ (path) => path[0] !== undefined && "key" in path[0],+ );+ if (objectPaths.length > 0 && typeof child === "object" && child !== null)+ result[key] = projectObjectPaths(child, objectPaths);+ }+ return result;+};++/** Union sliced rows by their original index, then emit them in source order. */+const projectArraySlices = (+ source: readonly unknown[],+ paths: readonly FilterPath[],+): unknown[] => {+ const selected = new Map<number, FilterPath[]>();+ for (const path of paths) {+ const [slice, ...tail] = path;+ if (slice === undefined || !("start" in slice)) continue;+ const start = normalizeSliceIndex(slice.start, source.length);+ const end = normalizeSliceIndex(slice.end, source.length);+ for (let index = start; index < end; index += 1) {+ const siblings = selected.get(index) ?? [];+ siblings.push(tail);+ selected.set(index, siblings);+ }+ }+ return [...selected.keys()]+ .sort((left, right) => left - right)+ .map((index) => {+ const siblings = selected.get(index)!;+ const item = source[index];+ if (siblings.some((path) => path.length === 0)) return item;+ return projectObjectPaths(item, siblings);+ });+};++const normalizeSliceIndex = (index: number, length: number): number => {+ const integer = Number.isNaN(index) ? 0 : Math.trunc(index);+ return integer < 0+ ? Math.max(length + integer, 0)+ : Math.min(integer, length);+};++/**+ * The streaming JSON writer requires JSON values in arrays. Copy only array+ * branches that need normalization so a parent selection never mutates its+ * source value.+ */+const normalizeJsonArrayValues = (source: unknown[]): unknown[] => {+ let result: unknown[] | undefined;+ for (let index = 0; index < source.length; index += 1) {+ const value: unknown = source[index];+ const normalized =+ value === undefined ||+ typeof value === "function" ||+ typeof value === "symbol"+ ? null+ : Array.isArray(value)+ ? normalizeJsonArrayValues(value)+ : value;+ if (normalized !== value) {+ result ??= source.slice();+ result[index] = normalized;}}+ return result ?? source;};diff --git a/tests/boundary/cli/filteredArrayOutput.test.ts b/tests/boundary/cli/filteredArrayOutput.test.tsindex 52deb77eb..726727dd2 100644--- a/tests/boundary/cli/filteredArrayOutput.test.ts+++ b/tests/boundary/cli/filteredArrayOutput.test.ts@@ -11,6 +11,48 @@ import {const source = [{ selected: 1 }, { other: 2 }, { selected: null }];+const freezeRecursively = <T>(value: T): T => {+ if (typeof value !== "object" || value === null || Object.isFrozen(value))+ return value;+ for (const child of Object.values(value)) freezeRecursively(child);+ return Object.freeze(value);+};++const runFilteredCli = async (+ value: unknown,+ format: "json" | "jsonl" | "yaml" | "toon" | "md",+ filter: string,+): Promise<string> => {+ const cli = Cli.create("filtered-array-test", { sync: false });+ cli.command("rows", { run: () => value });+ const args = ["rows", "--format", format, "--filter-output", filter];+ const chunks: Buffer[] = [];+ const output = createCliResultOutput(+ args,+ new Writable({+ write(chunk: Buffer, _encoding, callback) {+ chunks.push(chunk);+ callback();+ },+ }),+ );+ if (output === undefined) throw new Error("Missing CLI output owner");+ streamCliCommandResults(cli, output);+ let fallback = "";+ await cli.serve(args, {+ exit: (code) => {+ expect(code).toBe(0);+ },+ stdout: (text) => {+ if (!output.handled) fallback += text;+ },+ });+ expect(fallback).toBe("");+ expect(output.handled).toBe(true);+ expect(output.failed).toBe(false);+ return Buffer.concat(chunks).toString("utf8");+};+it.each((["json", "jsonl"] as const).flatMap((format) =>[false, true].map((full) => ({ format, full })),@@ -167,3 +209,107 @@ it("finishes JSONL after a bounded large record before a missing selection", asy]);expect(output.failed).toBe(false);});++const projectionSource = freezeRecursively([+ {+ rows: [+ { id: 0, left: "l0", right: "r0", retained: [0, 10] },+ { id: 1, left: "l1", right: "r1", retained: [1, 11] },+ { id: 2, left: "l2", right: "r2", retained: [2, 12] },+ { id: 3, left: "l3", right: "r3", retained: [3, 13] },+ ],+ },+]);++const overlappingPaths = [+ "rows[0,3].left,rows[1,4].right",+ "rows[1,4].right,rows[0,3].left",+] as const;++it.each(["json", "jsonl", "yaml", "toon", "md"] as const)(+ "unions overlapping sibling slices independent of path order in %s",+ async (format) => {+ const expected = [+ {+ rows: [+ { left: "l0" },+ { left: "l1", right: "r1" },+ { left: "l2", right: "r2" },+ { right: "r3" },+ ],+ },+ ];+ const outputs = await Promise.all(+ overlappingPaths.map((filter) =>+ runFilteredCli(projectionSource, format, filter),+ ),+ );+ expect(outputs[0]).toBe(outputs[1]);+ if (format === "json") expect(JSON.parse(outputs[0]!)).toEqual(expected);+ else if (format === "jsonl")+ expect(+ outputs[0]!+ .trimEnd()+ .split("\n")+ .map((line) => JSON.parse(line)),+ ).toEqual(expected);+ else expect(outputs[0]).toBe(`${Formatter.format(expected, format)}\n`);+ expect(projectionSource[0]?.rows[0]?.retained).toEqual([0, 10]);+ },+);++const parentAndChildPaths = [+ "rows[0,2],rows[1,3].right",+ "rows[1,3].right,rows[0,2]",+] as const;++it.each(["json", "jsonl", "yaml", "toon", "md"] as const)(+ "preserves missing selected array values in %s",+ async (format) => {+ const value = freezeRecursively([{ selected: 1 }, {}, { selected: null }]);+ const selected = [1, undefined, null];+ const output = await runFilteredCli(value, format, "selected");+ if (format === "json") expect(JSON.parse(output)).toEqual([1, null, null]);+ else if (format === "jsonl")+ expect(+ output+ .trimEnd()+ .split("\n")+ .map((line) => JSON.parse(line)),+ ).toEqual([1, null, null]);+ else expect(output).toBe(`${Formatter.format(selected, format)}\n`);+ expect(value).toEqual([{ selected: 1 }, {}, { selected: null }]);+ },+);++it.each(["json", "jsonl", "yaml", "toon", "md"] as const)(+ "lets whole-parent selection dominate child paths without mutating source in %s",+ async (format) => {+ const expected = [+ {+ rows: [+ projectionSource[0]!.rows[0],+ projectionSource[0]!.rows[1],+ { right: "r2" },+ ],+ },+ ];+ const before = structuredClone(projectionSource);+ const outputs = await Promise.all(+ parentAndChildPaths.map((filter) =>+ runFilteredCli(projectionSource, format, filter),+ ),+ );+ expect(outputs[0]).toBe(outputs[1]);+ if (format === "json") expect(JSON.parse(outputs[0]!)).toEqual(expected);+ else if (format === "jsonl")+ expect(+ outputs[0]!+ .trimEnd()+ .split("\n")+ .map((line) => JSON.parse(line)),+ ).toEqual(expected);+ else expect(outputs[0]).toBe(`${Formatter.format(expected, format)}\n`);+ expect(projectionSource).toEqual(before);+ },+);