2026-07-18 04:11:29 +00:00
import { z } from "incur" ;
2026-10-07 19:22:04 +08:00
import { compareManagedMemberPaths } from "../application/managed/ManagedMemberComparisonService.js" ;
import { verifyManagedNativeBoundariesEvidence } from "../application/managed/ManagedNativeVerificationService.js" ;
import { importManagedReconstructionEvidence } from "../application/managed/ManagedReconstructionService.js" ;
2026-10-09 20:02:52 +08:00
import type { DirectAnalysis } from "../composition/directAnalysis.js" ;
2026-10-12 14:46:37 +08:00
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js" ;
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js" ;
2026-07-18 04:11:29 +00:00
import { parseCliJsonInput } from "../cliJsonInput.js" ;
2026-10-12 14:46:37 +08:00
import { MANAGED_STATIC_PROVIDER } from "../application/InvestigationProviders.js" ;
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js" ;
2026-10-06 05:57:38 +08:00
import { projectAnalysisError } from "../domain/analysisErrorProjection.js" ;
2026-10-12 14:46:37 +08:00
import { ProviderCleanupError } from "../domain/providerCleanupError.js" ;
2026-07-18 04:11:29 +00:00
import { CLI_COMMANDS } from "../cliCommandNames.js" ;
import { logCliCommand } from "../cliLogging.js" ;
2026-10-09 22:45:18 +08:00
import type { Logger } from "pino" ;
2026-07-18 04:11:29 +00:00
import type { CliInstance } from "./types.js" ;
import { registerProjectManagedApplicationGraph } from "./managedProjectGraphCommand.js" ;
2026-10-12 04:26:32 -07:00
import { managedMembersInputSchema } from "../contracts/managed/managedToolContracts.js" ;
2026-07-18 04:11:29 +00:00
export const registerManagedCommands = (
cli : CliInstance ,
logger : Logger ,
2026-10-09 20:02:52 +08:00
runProviderAnalysis : DirectAnalysis [ "runProviderAnalysis" ],
2026-07-18 04:11:29 +00:00
) : void => {
2026-10-09 20:02:52 +08:00
registerInspectManagedArtifact ( cli , logger , runProviderAnalysis );
registerInspectManagedMembers ( cli , logger , runProviderAnalysis );
registerInspectManagedNativeBoundaries ( cli , logger , runProviderAnalysis );
2026-07-18 04:11:29 +00:00
registerCompareManagedMembers ( cli , logger );
registerImportManagedReconstruction ( cli , logger );
registerVerifyManagedNativeBoundaries ( cli , logger );
registerProjectManagedApplicationGraph ( cli , logger );
};
const registerInspectManagedArtifact = (
cli : CliInstance ,
logger : Logger ,
2026-10-09 20:02:52 +08:00
runProviderAnalysis : DirectAnalysis [ "runProviderAnalysis" ],
2026-07-18 04:11:29 +00:00
) : void => {
cli . command ( CLI_COMMANDS . inspectManagedArtifact , {
description : "Inspect PE/CLI identity without loading target code" ,
args : z.object ({
path : z.string (). describe ( "Managed PE executable or assembly path" ),
}),
2026-09-29 07:08:46 +08:00
run : ({ args }) =>
2026-07-18 04:11:29 +00:00
logCliCommand ( logger , "inspect-managed-artifact" , () =>
2026-09-29 07:08:46 +08:00
runProviderAnalysis ( args . path , "inspect_managed_artifact" , {}, logger ),
2026-07-18 04:11:29 +00:00
),
});
};
const registerInspectManagedMembers = (
cli : CliInstance ,
logger : Logger ,
2026-10-09 20:02:52 +08:00
runProviderAnalysis : DirectAnalysis [ "runProviderAnalysis" ],
2026-07-18 04:11:29 +00:00
) : void => {
cli . command ( CLI_COMMANDS . inspectManagedMembers , {
description :
"Inspect PE/CLI metadata members, signatures, and CIL anchors without loading target code" ,
args : z.object ({
path : z.string (). describe ( "Managed PE executable or assembly path" ),
}),
2026-10-11 19:31:37 -05:00
options : z.object ({
2026-10-12 04:26:32 -07:00
type : managedMembersInputSchema . shape . type_names . describe (
"Inspect only members declared by this exact type full name, such as System.Threading.Monitor; a nested type is named by its bare name, which selects every nested type of that name (repeatable)" ,
),
2026-10-11 19:31:37 -05:00
}),
run : ({ args , options }) =>
2026-07-18 04:11:29 +00:00
logCliCommand ( logger , "inspect-managed-members" , () =>
2026-10-11 19:31:37 -05:00
runProviderAnalysis (
args . path ,
"inspect_managed_members" ,
options . type === undefined ? {} : { type_names : options.type },
logger ,
),
2026-07-18 04:11:29 +00:00
),
});
};
const registerInspectManagedNativeBoundaries = (
cli : CliInstance ,
logger : Logger ,
2026-10-09 20:02:52 +08:00
runProviderAnalysis : DirectAnalysis [ "runProviderAnalysis" ],
2026-07-18 04:11:29 +00:00
) : void => {
cli . command ( CLI_COMMANDS . inspectManagedNativeBoundaries , {
description :
"Inspect PE/CLI PInvoke and native implementation boundary declarations without loading target code" ,
args : z.object ({
path : z.string (). describe ( "Managed PE executable or assembly path" ),
}),
2026-09-29 07:08:46 +08:00
run : ({ args }) =>
2026-07-18 04:11:29 +00:00
logCliCommand ( logger , "inspect-managed-native-boundaries" , () =>
runProviderAnalysis (
args . path ,
"inspect_managed_native_boundaries" ,
2026-09-29 07:08:46 +08:00
{},
2026-07-18 04:11:29 +00:00
logger ,
),
),
});
};
const compareManagedMemberArgs = z . object ({
leftPath : z.string (). describe ( "Baseline managed PE executable or assembly" ),
rightPath : z.string (). describe ( "Candidate managed PE executable or assembly" ),
});
const registerCompareManagedMembers = (
cli : CliInstance ,
logger : Logger ,
) : void => {
cli . command ( CLI_COMMANDS . compareManagedMembers , {
description :
2026-10-06 00:50:38 +08:00
"Compare managed PE/CLI member inventories by exact declared type, name, and signature; names alone never form a match" ,
2026-07-18 04:11:29 +00:00
args : compareManagedMemberArgs ,
2026-09-29 07:08:46 +08:00
run : ({ args }) =>
2026-07-18 04:11:29 +00:00
logCliCommand ( logger , "compare-managed-members" , async () => {
2026-10-12 14:46:37 +08:00
const resources = new ArtifactResourceScope ();
const result = await compareManagedMemberPaths (
{
leftPath : args.leftPath ,
rightPath : args.rightPath ,
},
{},
resources ,
);
try {
await resources . close ();
} catch ( cause : unknown ) {
const cleanup = ArtifactReaderFailure . cleanupObservation (
cause ,
"Managed member comparison resources" ,
);
const failure = new ProviderCleanupError (
MANAGED_STATIC_PROVIDER . id ,
cleanup . resources ,
{ reason : cleanup.reason },
{
cause ,
operation : "compare_managed_members" ,
...( result . ok ? { partialObservation : result.value } : {}),
},
);
const error = result . ok
? failure
: analysisErrorWithCleanupFailure (
result . error ,
failure ,
"compare_managed_members" ,
);
return {
error : "Managed member comparison failed" ,
... projectAnalysisError ( error ),
};
}
2026-07-18 04:11:29 +00:00
return result . ok
? result . value
: {
error : "Managed member comparison failed" ,
... projectAnalysisError ( result . error ),
};
}),
});
};
const registerImportManagedReconstruction = (
cli : CliInstance ,
logger : Logger ,
) : void => {
cli . command ( CLI_COMMANDS . importManagedReconstruction , {
description :
"Import decompiler-produced managed reconstruction against exact static member evidence" ,
args : z.object ({
inputJson : z
. string ()
. describe ( "Inline managed reconstruction JSON or JSON file path" ),
}),
run : ({ args }) =>
logCliCommand ( logger , "import-managed-reconstruction" , async () => {
const input = await parseCliJsonInput (
args . inputJson ,
"import-managed-reconstruction" ,
);
if ( ! input . ok ) return input . error ;
const result = importManagedReconstructionEvidence ( input . value );
return result . ok ? result.value : projectAnalysisError ( result . error );
}),
});
};
const registerVerifyManagedNativeBoundaries = (
cli : CliInstance ,
logger : Logger ,
) : void => {
cli . command ( CLI_COMMANDS . verifyManagedNativeBoundaries , {
description :
"Verify managed P/Invoke declarations against authenticated native Evidence" ,
args : z.object ({
inputJson : z
. string ()
. describe ( "Inline managed/native verification JSON or JSON file path" ),
}),
run : ({ args }) =>
logCliCommand ( logger , "verify-managed-native-boundaries" , async () => {
const input = await parseCliJsonInput (
args . inputJson ,
"verify-managed-native-boundaries" ,
);
if ( ! input . ok ) return input . error ;
const result = verifyManagedNativeBoundariesEvidence ( input . value );
return result . ok ? result.value : projectAnalysisError ( result . error );
}),
});
};