2026-07-12 07:44:21 +08:00
import { Cli , z } from "incur" ;
import { fileURLToPath } from "node:url" ;
import { runDoctor } from "./application/Doctor.js" ;
2026-07-12 21:12:18 +00:00
import {
runDirectAnalysis ,
runProviderAnalysis ,
} from "./application/DirectAnalysis.js" ;
2026-07-12 07:44:21 +08:00
import { runSetup } from "./application/Setup.js" ;
import { PRODUCT_IDENTITY } from "./identity.js" ;
2026-07-12 10:57:34 +08:00
import { createLogger , parseLogLevel , type Logger } from "./logger.js" ;
2026-07-12 21:12:18 +00:00
import { parseConfig } from "./config.js" ;
import {
exportEvidenceBundleCommand ,
importEvidenceBundleCommand ,
} from "./application/EvidenceBundleCommands.js" ;
2026-07-12 07:44:21 +08:00
2026-07-12 08:45:35 +08:00
/**
* Build the one-shot Incur CLI without starting Hopper at import time.
* Analysis commands acquire and close their own sessions; bare `mcp` and
* `--mcp` are intercepted by the executable dispatcher before this module loads.
*/
2026-07-12 07:44:21 +08:00
export const createCli = () : ReturnType < typeof Cli.create > => {
2026-07-12 10:57:34 +08:00
const logger = createLogger (
"cli" ,
process . env . REA_LOG_LEVEL === undefined
? "silent"
: parseLogLevel ( process . env . REA_LOG_LEVEL ),
);
2026-07-12 07:44:21 +08:00
const cli = Cli . create ( PRODUCT_IDENTITY . cliBinary , {
version : "0.1.0" ,
2026-07-12 20:49:30 +08:00
description :
"Reverse engineer anything from your terminal or coding agent." ,
2026-07-12 07:44:21 +08:00
mcp : {
command : PRODUCT_IDENTITY.mcpCommand ,
instructions :
2026-07-12 20:49:30 +08:00
"Ask what software, artifact, protocol, or behavior the user wants to understand, then choose the available investigation capabilities that can produce evidence." ,
2026-07-12 07:44:21 +08:00
},
sync : {
cwd : fileURLToPath ( new URL ( ".." , import . meta . url )),
include : [ "skills/*" ],
suggestions : [
2026-07-12 20:49:30 +08:00
"understand how a software feature works" ,
"investigate an artifact or observed behavior" ,
2026-07-12 12:26:16 +08:00
"check my REA setup" ,
2026-07-12 07:44:21 +08:00
],
},
});
cli . command ( "setup" , {
2026-07-12 12:26:16 +08:00
description : "Install requirements and configure coding agents" ,
2026-07-12 07:44:21 +08:00
options : z.object ({
yes : z
. boolean ()
. default ( false )
. describe ( "Approve prerequisite installation" ),
}),
alias : { yes : "y" },
2026-07-12 10:57:34 +08:00
run : ({ options }) =>
logCliCommand ( logger , "setup" , () => runSetup ( options . yes )),
2026-07-12 07:44:21 +08:00
});
cli . command ( "doctor" , {
2026-07-12 12:26:16 +08:00
description : "Check whether REA is ready" ,
2026-07-12 07:44:21 +08:00
options : z.object ({
2026-07-12 12:26:16 +08:00
target : z.string (). optional (). describe ( "Optional app path to check" ),
2026-07-12 07:44:21 +08:00
}),
2026-07-12 10:57:34 +08:00
run : ({ options }) =>
logCliCommand ( logger , "doctor" , () => runDoctor ( options . target )),
2026-07-12 07:44:21 +08:00
});
cli . command ( "analyze" , {
2026-07-12 12:26:16 +08:00
description : "Get an overview of an app" ,
2026-07-12 07:44:21 +08:00
args : z.object ({
2026-07-12 12:26:16 +08:00
path : z.string (). describe ( "App, program, or Hopper database path" ),
2026-07-12 07:44:21 +08:00
}),
2026-07-12 10:57:34 +08:00
run : ({ args }) =>
logCliCommand ( logger , "analyze" , () =>
runDirectAnalysis ( args . path , "binary_overview" , {}, logger ),
),
2026-07-12 07:44:21 +08:00
});
cli . command ( "decompile" , {
2026-07-12 12:26:16 +08:00
description : "Read one part of an app as code" ,
2026-07-12 07:44:21 +08:00
args : z.object ({
2026-07-12 12:26:16 +08:00
path : z.string (). describe ( "App or program path" ),
2026-07-12 07:44:21 +08:00
address : z.string (). describe ( "Procedure address" ),
}),
run : ({ args }) =>
2026-07-12 10:57:34 +08:00
logCliCommand ( logger , "decompile" , () =>
runDirectAnalysis (
args . path ,
"procedure_pseudo_code" ,
{ procedure : args.address },
logger ,
),
),
2026-07-12 07:44:21 +08:00
});
2026-07-12 21:12:18 +00:00
registerNativeCommands ( cli , logger );
registerArtifactCommands ( cli , logger );
registerEvidenceCommands ( cli , logger );
2026-07-12 07:44:21 +08:00
return cli ;
};
2026-07-12 10:57:34 +08:00
2026-07-12 21:12:18 +00:00
const registerArtifactCommands = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "inventory-artifact" , {
description : "Build a bounded deterministic artifact graph" ,
args : z.object ({
path : z.string (). describe ( "Application or package path" ),
}),
options : z.object ({
offset : z.number (). int (). min ( 0 ). default ( 0 ),
limit : z.number (). int (). min ( 1 ). max ( 500 ). default ( 100 ),
}),
run : ({ args , options }) =>
logCliCommand ( logger , "inventory-artifact" , () =>
runProviderAnalysis (
args . path ,
"inventory_artifact" ,
{
node_offset : options.offset ,
node_limit : options.limit ,
occurrence_offset : options.offset ,
occurrence_limit : options.limit ,
edge_offset : options.offset ,
edge_limit : options.limit ,
},
logger ,
),
),
});
cli . command ( "extract-artifact" , {
description : "Extract explicitly selected artifact occurrences safely" ,
args : z.object ({
path : z.string (). describe ( "Application or package path" ),
outputRoot : z.string (). describe ( "Absent absolute output root" ),
occurrenceIds : z
. array ( z . string (). regex ( /^occ_[a-f0-9]{64}$/u))
. min ( 1 )
. max ( 500 ),
}),
alias : { outputRoot : "output-root" , occurrenceIds : "occurrence-ids" },
run : ({ args }) =>
logCliCommand ( logger , "extract-artifact" , () =>
runProviderAnalysis (
args . path ,
"extract_artifact" ,
{
approved : true ,
output_root : args.outputRoot ,
occurrence_ids : args.occurrenceIds ,
},
logger ,
),
),
});
};
const registerNativeCommands = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
for ( const [ command , tool ] of [
[ "inspect-macho" , "inspect_macho" ],
[ "inspect-signature" , "inspect_signature" ],
[ "list-architectures" , "list_architectures" ],
] as const ) {
cli . command ( command , {
description : `Run ${ tool } without launching Hopper` ,
args : z.object ({ path : z.string (). describe ( "Mach-O or app path" ) }),
run : ({ args }) =>
logCliCommand ( logger , command , () =>
runProviderAnalysis ( args . path , tool , {}, logger ),
),
});
}
cli . command ( "inspect-plist" , {
description : "Parse app plist metadata without launching Hopper" ,
args : z.object ({ path : z.string (). describe ( "App or Mach-O path" ) }),
options : z.object ({
relativePath : z.string (). default ( "Contents/Info.plist" ),
}),
alias : { relativePath : "relative-path" },
run : ({ args , options }) =>
logCliCommand ( logger , "inspect-plist" , () =>
runProviderAnalysis (
args . path ,
"inspect_plist" ,
{ relative_path : options.relativePath },
logger ,
),
),
});
cli . command ( "demangle-swift" , {
description : "Demangle a bounded Swift symbol batch without Hopper" ,
args : z.object ({
path : z.string (). describe ( "Artifact path used for evidence identity" ),
symbols : z.array ( z . string (). min ( 1 )). min ( 1 ). max ( 500 ),
}),
run : ({ args }) =>
logCliCommand ( logger , "demangle-swift" , () =>
runProviderAnalysis (
args . path ,
"demangle_swift" ,
{ symbols : args.symbols },
logger ,
),
),
});
};
const registerEvidenceCommands = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "evidence-import" , {
description : "Validate and import a bounded local Evidence v2 bundle" ,
args : z.object ({
path : z.string (). describe ( "Evidence bundle JSON path" ),
}),
run : ({ args }) =>
logCliCommand ( logger , "evidence-import" , async () => {
const config = parseConfig ( process . env );
if ( ! config . ok )
return { error : config.error._tag , message : config.error.message };
const imported = await importEvidenceBundleCommand (
args . path ,
config . value . evidenceFilePolicy ,
);
return imported . ok
? imported . value
: { error : imported.error._tag , message : imported.error.message };
}),
});
cli . command ( "evidence-export" , {
description : "Validate and atomically export canonical Evidence v2 JSON" ,
args : z.object ({
source : z.string (). describe ( "Existing evidence bundle JSON path" ),
output : z.string (). describe ( "Canonical output JSON path" ),
}),
options : z.object ({
overwrite : z.boolean (). default ( false ). describe ( "Replace output file" ),
}),
run : ({ args , options }) =>
logCliCommand ( logger , "evidence-export" , async () => {
const config = parseConfig ( process . env );
if ( ! config . ok )
return { error : config.error._tag , message : config.error.message };
const exported = await exportEvidenceBundleCommand (
args . source ,
args . output ,
options . overwrite ,
config . value . evidenceFilePolicy ,
);
return exported . ok
? exported . value
: { error : exported.error._tag , message : exported.error.message };
}),
});
};
2026-07-12 10:57:34 +08:00
const logCliCommand = async < Value >(
logger : Logger ,
command : string ,
execute : () => Promise < Value >,
) : Promise < Value > => {
const startedAt = performance . now ();
try {
const value = await execute ();
logger . info (
{
command ,
durationMs : Math.round (( performance . now () - startedAt ) * 100 ) / 100 ,
status : "ok" ,
},
"CLI command completed" ,
);
return value ;
} catch ( cause : unknown ) {
logger . error (
{
command ,
durationMs : Math.round (( performance . now () - startedAt ) * 100 ) / 100 ,
status : "error" ,
},
"CLI command failed" ,
);
throw cause ;
}
};