2026-07-12 07:44:21 +08:00
import { Cli , z } from "incur" ;
import { fileURLToPath } from "node:url" ;
2026-07-13 15:13:45 +08:00
import { createInterface } from "node:readline/promises" ;
2026-07-12 07:44:21 +08:00
import { runDoctor } from "./application/Doctor.js" ;
2026-07-12 21:12:18 +00:00
import {
runDirectAnalysis ,
runProviderAnalysis ,
2026-07-13 03:15:01 +00:00
runSessionStatus ,
2026-07-12 21:12:18 +00:00
} from "./application/DirectAnalysis.js" ;
2026-07-13 15:13:45 +08:00
import { runSetup , type SetupAction } from "./application/Setup.js" ;
2026-07-12 23:42:20 +00:00
import { runUninstall } from "./application/Uninstall.js" ;
2026-07-13 05:30:39 +00:00
import { runUpgrade , systemUpgradeHost } from "./application/Upgrade.js" ;
2026-07-12 07:44:21 +08:00
import { PRODUCT_IDENTITY } from "./identity.js" ;
2026-07-12 10:57:34 +08:00
import { createLogger , parseLogLevel , type Logger } from "./logger.js" ;
2026-07-13 03:15:01 +00:00
import { logCliCommand } from "./cliLogging.js" ;
2026-07-12 21:12:18 +00:00
import { parseConfig } from "./config.js" ;
2026-07-12 21:34:05 +00:00
import { importReferenceSource } from "./application/ReferenceSourceImport.js" ;
2026-07-13 03:15:01 +00:00
import { registerEvidenceCommands } from "./cliEvidenceCommands.js" ;
2026-07-13 17:01:09 +08:00
import { registerProcessCommands } from "./cliProcessCommands.js" ;
2026-07-13 19:37:54 +08:00
import { registerInvestigationCommands } from "./cliInvestigationCommands.js" ;
2026-07-14 01:12:27 +00:00
import {
AnalysisProtocolError ,
PermissionRequiredError ,
projectAnalysisError ,
} from "./domain/errors.js" ;
2026-07-13 19:09:27 +00:00
import { projectReferenceSourceImportError } from "./application/ReferenceSourceImportTypes.js" ;
2026-07-14 01:12:27 +00:00
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js" ;
import { registerPolicyCommands } from "./cliPolicyCommands.js" ;
2026-07-14 12:27:13 +08:00
import { registerBrowserCommands } from "./cliBrowserCommands.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 , {
2026-07-14 01:12:27 +00:00
version : PRODUCT_IDENTITY.packageVersion ,
2026-07-13 19:17:15 +00:00
description : "Reverse engineer anything from your terminal or 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
],
},
});
2026-07-13 18:20:40 +08:00
registerSetupCommands ( cli , logger );
2026-07-13 03:15:01 +00:00
registerCoreCommands ( cli , logger );
registerFunctionCommand ( cli , logger );
registerSearchCommand ( cli , logger );
registerXrefsCommand ( cli , logger );
registerTraceCommand ( cli , logger );
registerCapabilityCommands ( cli , logger );
registerNativeCommands ( cli , logger );
registerArtifactCommands ( cli , logger );
2026-07-13 19:37:54 +08:00
registerInvestigationCommands ( cli , logger );
2026-07-13 03:15:01 +00:00
registerEvidenceCommands ( cli , logger );
registerReferenceSourceCommand ( cli , logger );
2026-07-13 17:01:09 +08:00
registerProcessCommands ( cli , logger );
2026-07-14 01:12:27 +00:00
registerPolicyCommands ( cli , logger );
2026-07-14 12:27:13 +08:00
registerBrowserCommands ( cli , logger );
2026-07-13 03:15:01 +00:00
return cli ;
};
const registerCoreCommands = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
const overviewOptions = z . object ({
detail : z.enum ([ "concise" , "detailed" ]). default ( "concise" ),
limit : z.number (). int (). min ( 1 ). max ( 50 ). default ( 10 ),
2026-07-14 04:18:38 +08:00
snapshot : z
. string ()
. min ( 1 )
. optional ()
. describe ( "Load and update a local analysis snapshot" ),
2026-07-13 03:15:01 +00:00
});
2026-07-13 18:20:40 +08:00
cli . command ( "analyze" , {
description : "Get an overview of an app" ,
args : z.object ({
path : z.string (). describe ( "App, program, or Hopper database path" ),
}),
options : overviewOptions ,
run : ({ args , options }) =>
logCliCommand ( logger , "analyze" , () =>
runDirectAnalysis (
args . path ,
"binary_overview" ,
{ detail : options.detail , limit : options.limit },
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 18:20:40 +08:00
),
),
});
cli . command ( "inspect" , {
description : "Inspect an app overview with evidence" ,
args : z.object ({
path : z.string (). describe ( "App, program, or Hopper database path" ),
}),
options : overviewOptions ,
run : ({ args , options }) =>
logCliCommand ( logger , "inspect" , () =>
runDirectAnalysis (
args . path ,
"binary_overview" ,
{ detail : options.detail , limit : options.limit },
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 18:20:40 +08:00
),
),
});
cli . command ( "decompile" , {
description : "Read one part of an app as code" ,
args : z.object ({
path : z.string (). describe ( "App or program path" ),
address : z.string (). describe ( "Procedure address" ),
}),
2026-07-14 04:18:38 +08:00
options : z.object ({ snapshot : z.string (). min ( 1 ). optional () }),
run : ({ args , options }) =>
2026-07-13 18:20:40 +08:00
logCliCommand ( logger , "decompile" , () =>
runDirectAnalysis (
args . path ,
"procedure_pseudo_code" ,
{ procedure : args.address },
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 18:20:40 +08:00
),
),
});
2026-07-13 19:37:54 +08:00
};
2026-07-13 18:20:40 +08:00
const registerSetupCommands = (
2026-07-13 19:37:54 +08:00
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
2026-07-12 07:44:21 +08:00
cli . command ( "setup" , {
2026-07-13 19:17:15 +00:00
description : "Install requirements and configure agents" ,
2026-07-12 07:44:21 +08:00
options : z.object ({
yes : z
. boolean ()
. default ( false )
2026-07-13 15:13:45 +08:00
. describe ( "Approve user-owned setup actions without prompting" ),
installHopper : z
. boolean ()
. default ( false )
. describe ( "Also approve Hopper installation with --yes" ),
2026-07-12 07:44:21 +08:00
}),
2026-07-13 15:13:45 +08:00
alias : { yes : "y" , installHopper : "install-hopper" },
run : ({ options , formatExplicit }) =>
logCliCommand ( logger , "setup" , () =>
runSetup (
{
approved : options.yes ,
installHopper : options.installHopper ,
structured : formatExplicit ,
},
undefined ,
options . yes || formatExplicit || process . stdin . isTTY !== true
? undefined
: confirmSetup ,
),
),
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
});
2026-07-12 23:42:20 +00:00
cli . command ( "uninstall" , {
description : "Remove REA-owned agent configuration and skill files" ,
options : z.object ({
purgeData : z
. boolean ()
. default ( false )
. describe ( "Also remove REA caches and state" ),
}),
alias : { purgeData : "purge-data" },
run : ({ options }) =>
logCliCommand ( logger , "uninstall" , () => runUninstall ( options . purgeData )),
});
2026-07-13 05:30:39 +00:00
cli . command ( "upgrade" , {
description : "Upgrade a global npm installation to the latest REA release" ,
run : ({ formatExplicit }) =>
logCliCommand ( logger , "upgrade" , () =>
runUpgrade (
2026-07-14 01:12:27 +00:00
PRODUCT_IDENTITY . packageVersion ,
2026-07-13 05:30:39 +00:00
systemUpgradeHost (),
formatExplicit ? "structured" : "human" ,
),
),
});
2026-07-13 19:37:54 +08:00
};
2026-07-13 15:13:45 +08:00
const confirmSetup = async (
actions : readonly SetupAction [],
) : Promise < boolean > => {
process . stdout . write ( "\nREA setup plan\n" );
for ( const action of actions )
process . stdout . write (
` - ${ action . detail } \ n ${ action . target }${ action . external ? " (external software)" : "" } \ n` ,
);
const prompt = createInterface ({
input : process.stdin ,
output : process.stdout ,
});
try {
const answer = ( await prompt . question ( "Continue? [Y/n] " ))
. trim ()
. toLowerCase ();
return answer === "" || answer === "y" || answer === "yes" ;
} finally {
prompt . close ();
}
};
2026-07-13 03:15:01 +00:00
const registerXrefsCommand = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "xrefs" , {
description : "List bounded references to an analyzed address" ,
args : z.object ({
path : z.string (). describe ( "App or program path" ),
address : z.string (). describe ( "Hexadecimal address" ),
}),
2026-07-14 04:18:38 +08:00
options : z.object ({ snapshot : z.string (). min ( 1 ). optional () }),
run : ({ args , options }) =>
2026-07-13 03:15:01 +00:00
logCliCommand ( logger , "xrefs" , () =>
runDirectAnalysis (
args . path ,
"xrefs" ,
{ address : args.address },
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 03:15:01 +00:00
),
),
});
};
const registerTraceCommand = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "trace" , {
description : "Trace a bounded literal feature through analyzed references" ,
args : z.object ({
path : z.string (). describe ( "App or program path" ),
query : z.string (). min ( 1 ). describe ( "Literal feature query" ),
}),
options : z.object ({
caseSensitive : z.boolean (). default ( false ),
limit : z.number (). int (). min ( 1 ). max ( 100 ). default ( 20 ),
maxOperations : z.number (). int (). min ( 1 ). max ( 100 ). default ( 20 ),
2026-07-14 04:18:38 +08:00
snapshot : z.string (). min ( 1 ). optional (),
2026-07-13 03:15:01 +00:00
}),
alias : {
caseSensitive : "case-sensitive" ,
maxOperations : "max-operations" ,
},
run : ({ args , options }) =>
logCliCommand ( logger , "trace" , () =>
runDirectAnalysis (
args . path ,
"trace_feature" ,
{
query : args.query ,
case_sensitive : options.caseSensitive ,
limit : options.limit ,
max_operations : options.maxOperations ,
},
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 03:15:01 +00:00
),
),
});
};
const registerCapabilityCommands = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
for ( const command of [ "capabilities" , "providers" ] as const ) {
cli . command ( command , {
description :
command === "capabilities"
? "List provider capabilities and side effects"
: "List configured analysis providers" ,
run : () => logCliCommand ( logger , command , () => runSessionStatus ( logger )),
});
}
};
const registerFunctionCommand = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "function" , {
description : "Analyze one bounded function with evidence" ,
args : z.object ({
path : z.string (). describe ( "App or program path" ),
address : z.string (). describe ( "Procedure name or address" ),
}),
options : z.object ({
includeAssembly : z.boolean (). default ( false ),
limit : z.number (). int (). min ( 1 ). max ( 500 ). default ( 100 ),
maxPseudocodeChars : z.number (). int (). min ( 1 ). max ( 100 _000 ). default ( 20 _000 ),
maxInstructions : z.number (). int (). min ( 1 ). max ( 5 _000 ). default ( 500 ),
2026-07-14 04:18:38 +08:00
snapshot : z.string (). min ( 1 ). optional (),
2026-07-13 03:15:01 +00:00
}),
alias : {
includeAssembly : "include-assembly" ,
maxPseudocodeChars : "max-pseudocode-chars" ,
maxInstructions : "max-instructions" ,
},
run : ({ args , options }) =>
logCliCommand ( logger , "function" , () =>
runDirectAnalysis (
args . path ,
"analyze_function" ,
{
procedure : args.address ,
include_assembly : options.includeAssembly ,
limit : options.limit ,
max_pseudocode_chars : options.maxPseudocodeChars ,
max_instructions : options.maxInstructions ,
},
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 03:15:01 +00:00
),
),
});
};
const registerSearchCommand = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "search" , {
description : "Search bounded analyzed strings or procedure names" ,
args : z.object ({
path : z.string (). describe ( "App or program path" ),
pattern : z.string (). min ( 1 ). describe ( "Literal text or regex pattern" ),
}),
options : z.object ({
kind : z.enum ([ "strings" , "procedures" ]). default ( "strings" ),
mode : z.enum ([ "literal" , "regex" ]). default ( "literal" ),
caseSensitive : z.boolean (). default ( false ),
offset : z.number (). int (). min ( 0 ). default ( 0 ),
limit : z.number (). int (). min ( 1 ). max ( 100 ). default ( 100 ),
2026-07-14 04:18:38 +08:00
snapshot : z.string (). min ( 1 ). optional (),
2026-07-13 03:15:01 +00:00
}),
alias : { caseSensitive : "case-sensitive" },
run : ({ args , options }) =>
logCliCommand ( logger , "search" , () =>
runDirectAnalysis (
args . path ,
options . kind === "strings" ? "search_strings" : "search_procedures" ,
{
pattern : args.pattern ,
mode : options.mode ,
case_sensitive : options.caseSensitive ,
offset : options.offset ,
limit : options.limit ,
},
2026-07-14 04:18:38 +08:00
{ logger , snapshotPath : options.snapshot },
2026-07-13 03:15:01 +00:00
),
),
});
2026-07-12 07:44:21 +08:00
};
2026-07-12 10:57:34 +08:00
2026-07-12 21:34:05 +00:00
const registerReferenceSourceCommand = (
cli : ReturnType < typeof Cli.create >,
logger : Logger ,
) : void => {
cli . command ( "import-reference-source" , {
description : "Import a bounded source tree as historical reference only" ,
args : z.object ({
root : z
. string ()
. describe ( "Source root allowed by REA_REFERENCE_ROOTS_JSON" ),
}),
run : ({ args }) =>
logCliCommand ( logger , "import-reference-source" , async () => {
const config = parseConfig ( process . env );
if ( ! config . ok )
2026-07-13 11:51:37 +00:00
return {
error : "Import failed" ,
... projectAnalysisError ( config . error ),
};
2026-07-14 01:12:27 +00:00
const authority = await loadConfiguredPermissionAuthority ( config . value );
if ( ! authority . ok )
return {
error : "Import failed" ,
... projectAnalysisError ( authority . error ),
};
const authorized = await authority . value . authorize (
{
capability : "reference_read" ,
roots : [ args . root ],
executables : [],
environment_names : [],
network : "none" ,
mount : false ,
operation_identity : `import_reference_source: ${ args . root } ` ,
},
"read" ,
);
if ( ! authorized . ok ) {
const error =
authorized . error instanceof PermissionRequiredError
? authorized.error
: new AnalysisProtocolError ( authorized . error . message , {
cause : authorized.error ,
});
return {
error : "Import failed" ,
... projectAnalysisError ( error ),
};
}
2026-07-12 21:34:05 +00:00
const imported = await importReferenceSource ({
root : args.root ,
caller : "rea-cli" ,
policy : config.value.referenceSourcePolicy ,
importer : PRODUCT_IDENTITY.packageName ,
importerVersion : null ,
});
return imported . ok
? imported . value
2026-07-13 19:09:27 +00:00
: {
error : "Import failed" ,
... projectReferenceSourceImportError ( imported . error ),
};
2026-07-12 21:34:05 +00: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 ),
2026-07-14 01:12:27 +00:00
integrityPolicy : z.enum ([ "fail" , "record-and-continue" ]). default ( "fail" ),
integrityContinueApproved : z.boolean (). default ( false ),
maxIntegrityMismatches : z.number (). int (). min ( 1 ). max ( 100 ). default ( 10 ),
nativeMountApproved : z.boolean (). default ( false ),
2026-07-12 21:12:18 +00:00
}),
2026-07-14 01:12:27 +00:00
alias : {
integrityPolicy : "integrity-policy" ,
integrityContinueApproved : "integrity-continue-approved" ,
maxIntegrityMismatches : "max-integrity-mismatches" ,
nativeMountApproved : "native-mount-approved" ,
},
2026-07-12 21:12:18 +00:00
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 ,
2026-07-14 01:12:27 +00:00
integrity_policy : options.integrityPolicy ,
integrity_continue_approved : options.integrityContinueApproved ,
max_integrity_mismatches : options.maxIntegrityMismatches ,
native_mount_approved : options.nativeMountApproved ,
2026-07-12 21:12:18 +00:00
},
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 ,
),
),
});
};