Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

refactor(json): reuse shared parser at CLI and shape boundaries

morluto4f5437983143
diff --git a/src/cli/gpuCommands.ts b/src/cli/gpuCommands.ts
index 93f18a84c..e42d2066f 100644
--- a/src/cli/gpuCommands.ts
+++ b/src/cli/gpuCommands.ts
@@ -2,6 +2,7 @@ import { inspectGpuCodeObjectsInputSchema } from "../domain/gpu/gpuCodeObjects.j
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
+import { safeParseJson } from "../domain/safeJson.js";
import { z } from "incur";
import type { Logger } from "pino";
import type { CliInstance } from "./types.js";
@@ -59,19 +60,23 @@ export const registerGpuCommands = (cli: CliInstance, logger: Logger): void => {
for (const name of ["object_ids", "targets", "view"] as const) {
const value = options[name];
if (value === undefined) continue;
- try {
- selectors[name] = JSON.parse(value);
- } catch (cause) {
+ const decoded = safeParseJson(value);
+ if (!decoded.ok) {
return projectAnalysisError(
- new AnalysisInputError("inspect_gpu_code_objects", { cause }, [
- {
- path: [name],
- reason: "invalid_format",
- message: `${name} must be valid JSON`,
- },
- ]),
+ new AnalysisInputError(
+ "inspect_gpu_code_objects",
+ { cause: decoded.cause },
+ [
+ {
+ path: [name],
+ reason: "invalid_format",
+ message: `${name} must be valid JSON`,
+ },
+ ],
+ ),
);
}
+ selectors[name] = decoded.value;
}
const rawInput = {
path: absoluteCallerPath(args.path),
diff --git a/src/cli/modelCommands.ts b/src/cli/modelCommands.ts
index 6230937a3..e6b3803bf 100644
--- a/src/cli/modelCommands.ts
+++ b/src/cli/modelCommands.ts
@@ -2,6 +2,7 @@ import { inspectModelArtifactInputSchema } from "../domain/model/modelArtifactQu
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
+import { safeParseJson } from "../domain/safeJson.js";
import { z } from "incur";
import type { Logger } from "pino";
import type { CliInstance } from "./types.js";
@@ -44,21 +45,26 @@ export const registerModelCommands = (
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, CLI_COMMANDS.inspectModelArtifact, async () => {
- let view: unknown;
- try {
- view =
- options.view === undefined ? undefined : JSON.parse(options.view);
- } catch (cause) {
+ const decoded =
+ options.view === undefined
+ ? { ok: true as const, value: undefined }
+ : safeParseJson(options.view);
+ if (!decoded.ok) {
return projectAnalysisError(
- new AnalysisInputError("inspect_model_artifact", { cause }, [
- {
- path: ["view"],
- reason: "invalid_format",
- message: "view must be a JSON model page selection",
- },
- ]),
+ new AnalysisInputError(
+ "inspect_model_artifact",
+ { cause: decoded.cause },
+ [
+ {
+ path: ["view"],
+ reason: "invalid_format",
+ message: "view must be a JSON model page selection",
+ },
+ ],
+ ),
);
}
+ const view = decoded.value;
const selected = inspectModelArtifactInputSchema.safeParse({
path: absoluteCallerPath(args.path),
hash: options.hash,
diff --git a/src/cli/rustCommands.ts b/src/cli/rustCommands.ts
index e71546d5c..2dbb44bfc 100644
--- a/src/cli/rustCommands.ts
+++ b/src/cli/rustCommands.ts
@@ -10,6 +10,7 @@ import { inspectRustBinaryRequestSchema } from "../domain/rust/rustBinaryQuery.j
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
+import { safeParseJson } from "../domain/safeJson.js";
import type { Logger } from "pino";
import type { CliInstance } from "./types.js";
import { withCommandCancellation } from "./commandCancellation.js";
@@ -44,21 +45,26 @@ export const registerRustCommands = (
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, CLI_COMMANDS.inspectRustBinary, async () => {
- let view: unknown;
- try {
- view =
- options.view === undefined ? undefined : JSON.parse(options.view);
- } catch (cause) {
+ const decoded =
+ options.view === undefined
+ ? { ok: true as const, value: undefined }
+ : safeParseJson(options.view);
+ if (!decoded.ok) {
return projectAnalysisError(
- new AnalysisInputError("inspect_rust_binary", { cause }, [
- {
- path: ["view"],
- reason: "invalid_format",
- message: "view must be a JSON package or edge selection",
- },
- ]),
+ new AnalysisInputError(
+ "inspect_rust_binary",
+ { cause: decoded.cause },
+ [
+ {
+ path: ["view"],
+ reason: "invalid_format",
+ message: "view must be a JSON package or edge selection",
+ },
+ ],
+ ),
);
}
+ const view = decoded.value;
const input = inspectRustBinaryRequestSchema.safeParse({
path: absoluteCallerPath(args.path),
...(options.detail === undefined ? {} : { detail: options.detail }),
diff --git a/src/domain/jsonShape.ts b/src/domain/jsonShape.ts
index ba5292abb..e18701795 100644
--- a/src/domain/jsonShape.ts
+++ b/src/domain/jsonShape.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
+import { safeParseJson } from "./safeJson.js";
const jsonValueTypeSchema = z.enum([
"object",
@@ -133,13 +134,8 @@ export const inferJsonShape = (text: string): JsonShape | null => {
export const parseJsonObject = (
text: string,
): Readonly<Record<string, unknown>> | null => {
- let root: unknown;
- try {
- root = JSON.parse(text);
- } catch {
- return null;
- }
- return isRecord(root) ? root : null;
+ const parsed = safeParseJson(text);
+ return parsed.ok && isRecord(parsed.value) ? parsed.value : null;
};
/** The JSON type name of one parsed value. */
export const jsonValueType = (value: unknown): JsonValueType => {
diff --git a/tests/acceptance/modelArtifactCli.test.ts b/tests/acceptance/modelArtifactCli.test.ts
index 7130fcc51..ac2450fd6 100644
--- a/tests/acceptance/modelArtifactCli.test.ts
+++ b/tests/acceptance/modelArtifactCli.test.ts
@@ -483,3 +483,30 @@ cliTest(
});
},
);
+
+cliTest(
+ "rejects malformed model view JSON before opening the selected path",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-model-view-json-");
+ const missing = join(root, "not-created.gguf");
+ const result = await cli.run({
+ arguments: ["inspect-model-artifact", missing, "--view", "{", "--json"],
+ });
+
+ expect(result.exitCode).toBe(1);
+ expect(result.json).toMatchObject({
+ code: "invalid_request",
+ details: {
+ operation: "inspect_model_artifact",
+ issues: [
+ {
+ path: ["view"],
+ reason: "invalid_format",
+ message: "view must be a JSON model page selection",
+ },
+ ],
+ },
+ });
+ expect(JSON.stringify(result.json)).not.toContain("ENOENT");
+ },
+);