feat: evolve REA analysis platform

This commit is contained in:
morluto
2026-07-12 19:32:13 +08:00
parent 1f235cb52f
commit 4f18e26ee5
47 changed files with 1841 additions and 334 deletions
+22 -2
View File
@@ -1,4 +1,6 @@
import { constants } from "node:fs";
import { createReadStream } from "node:fs";
import { createHash } from "node:crypto";
import {
access,
open,
@@ -25,6 +27,7 @@ type BinaryArchitecture = "x86" | "x86_64" | "arm" | "arm64";
*/
export interface BinaryTarget {
readonly path: string;
readonly sha256: string;
readonly kind: "executable" | "database";
readonly format: "hopper" | "mach-o" | "elf" | "pe";
readonly architecture?: BinaryArchitecture;
@@ -56,7 +59,13 @@ export const parseBinaryTarget = async (
targetKind === "database" ||
(targetKind === undefined && path.toLowerCase().endsWith(".hop"))
)
return ok({ path, kind: "database", format: "hopper", loaderArgs: [] });
return ok({
path,
sha256: await sha256File(path),
kind: "database",
format: "hopper",
loaderArgs: [],
});
const handle = await open(path, "r");
let detected: Result<ExecutableMetadata, string>;
try {
@@ -65,7 +74,12 @@ export const parseBinaryTarget = async (
await handle.close();
}
if (!detected.ok) return err(new BinaryTargetError(path, detected.error));
return ok({ path, kind: "executable", ...detected.value });
return ok({
path,
sha256: await sha256File(path),
kind: "executable",
...detected.value,
});
} catch (cause: unknown) {
return err(
new BinaryTargetError(candidate, "path is not readable", { cause }),
@@ -73,6 +87,12 @@ export const parseBinaryTarget = async (
}
};
const sha256File = async (path: string): Promise<string> => {
const hash = createHash("sha256");
for await (const chunk of createReadStream(path)) hash.update(chunk);
return hash.digest("hex");
};
const resolveAppBundle = async (
path: string,
): Promise<Result<string, BinaryTargetError>> => {
+13 -5
View File
@@ -1,8 +1,16 @@
/** Base class for expected Hopper integration failures. */
export abstract class HopperError extends Error {
/** Base class for expected analysis, provider, and session failures. */
export abstract class AnalysisError extends Error {
abstract readonly _tag: string;
}
/** Base class for failures produced specifically by the Hopper provider. */
export abstract class HopperError extends AnalysisError {}
/** Provider-neutral invalid analysis input or output at an application boundary. */
export class AnalysisProtocolError extends AnalysisError {
readonly _tag = "HopperProtocolError";
}
/** Hopper did not respond within the configured operation deadline. */
export class HopperTimeoutError extends HopperError {
readonly _tag = "HopperTimeoutError";
@@ -57,12 +65,12 @@ export class HopperStartError extends HopperError {
}
/** Runtime configuration could not be parsed safely. */
export class ConfigurationError extends HopperError {
export class ConfigurationError extends AnalysisError {
readonly _tag = "ConfigurationError";
}
/** No app or binary session exists for an analysis request. */
export class NoBinaryOpenError extends HopperError {
export class NoBinaryOpenError extends AnalysisError {
readonly _tag = "NoBinaryOpenError";
constructor() {
super(
@@ -72,7 +80,7 @@ export class NoBinaryOpenError extends HopperError {
}
/** A supplied target path could not be safely opened as a supported app or binary. */
export class BinaryTargetError extends HopperError {
export class BinaryTargetError extends AnalysisError {
readonly _tag = "BinaryTargetError";
constructor(
readonly path: string,
+62
View File
@@ -0,0 +1,62 @@
import { z } from "zod";
import type { BinaryTarget } from "./binaryTarget.js";
import { jsonValueSchema, type JsonValue } from "./jsonValue.js";
const artifactEvidenceSchema = z.object({
path: z.string(),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
format: z.enum(["hopper", "mach-o", "elf", "pe"]),
architecture: z.enum(["x86", "x86_64", "arm", "arm64"]).nullable(),
});
/** Strict JSON representation of one successful public analysis observation. */
export const evidenceSchema = z.object({
schema_version: z.literal(1),
artifact: artifactEvidenceSchema.nullable(),
provider: z.object({ id: z.literal("hopper"), version: z.null() }),
operation: z.string().min(1),
parameters: z.record(z.string(), jsonValueSchema),
result: jsonValueSchema,
confidence: z.literal("observed"),
limitations: z.array(z.string()),
});
export type Evidence = z.infer<typeof evidenceSchema>;
export interface EvidenceObservation {
readonly operation: string;
readonly parameters: Readonly<Record<string, JsonValue>>;
readonly result: JsonValue;
readonly limitations?: readonly string[];
}
/** Build deterministic evidence without claiming unavailable provider metadata. */
export const createEvidence = (
target: BinaryTarget | undefined,
observation: EvidenceObservation,
): Evidence =>
evidenceSchema.parse({
schema_version: 1,
artifact:
target === undefined
? null
: {
path: target.path,
sha256: target.sha256,
format: target.format,
architecture: target.architecture ?? null,
},
provider: { id: "hopper", version: null },
operation: observation.operation,
parameters: observation.parameters,
result: observation.result,
confidence: "observed",
limitations:
target === undefined
? [
"Artifact identity is unavailable for this fixed-target adapter.",
...(observation.limitations ?? []),
]
: [...(observation.limitations ?? [])],
});
+1 -1
View File
@@ -1,6 +1,6 @@
import { z } from "zod";
import type { JsonValue } from "../hopper/protocol.js";
import type { JsonValue } from "./jsonValue.js";
import { HopperProtocolError } from "./errors.js";
import { err, ok, type Result } from "./result.js";
+6
View File
@@ -0,0 +1,6 @@
import { z } from "zod";
/** JSON-safe value shared by domain, application, and adapter boundaries. */
export const jsonValueSchema = z.json();
export type JsonValue = z.infer<typeof jsonValueSchema>;
+1 -1
View File
@@ -1,4 +1,4 @@
import type { JsonValue } from "../hopper/protocol.js";
import type { JsonValue } from "./jsonValue.js";
import type { AddressedName } from "./hopperValues.js";
const MAX_SCANNED_SYMBOLS = 5_000;