a
    ãƒõjyƒ  ã                   @  sü  U d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ h d£Zh d£Zd	h d
£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœd h d!£ddœd"h d#£ddœd$h d%£ddœd&h d'£ddœd(h d)£ddœd*h d+£ddœd,h d-£ddœd.h d/£ddœd0h d1£ddœd2h d3£ddœd4h d5£ddœd6h d7£ddœd8œZd9ed:< d;d<d=d;d>d?œd=d<d@dAdBdCœdDdEd@dAdFdGœd@d@dBdAdAdHœdId>dAdJdKœd=d@d<dAdLœd;d@dAdAdMœdNdJdFdOœdPœZdQedR< h dS£ZdTdU„ eddVƒD ƒZdWdXdYdZgd[d\h d]£dd^d_œd`dadbgdcddh de£ddfd_œdgdag dh¢didjh dk£ddld_œdmdXg dn¢dodph dq£ddrœdsdtdugdvdwh dx£ddrœdydXg dz¢d{d|h d}£dd~d_œfZded€< d�d‚œdƒd„„Zd…d†d‡œdˆd‰„ZdŠd‹dŒœd�dŽ„ZdŠd�dŒœd�d‘„ZdŠd‹dŒœd’d“„Zd”d•d–d”d—d—d˜d™œZdšed›< dœd�dBdBdžœZdšedŸ< d d d¡d¡d¢d£œZdšed¤< d¥Zd†d¦d§œd¨d©„Zdªd‹d«œd¬d­„Z d†d‹d®œd¯d°„Z!d‹d†d±d²œd³d´„Z"d‹d†dµd²œd¶d·„Z#d‹d�d¸œd¹dº„Z$dªdšd»d¼œd½d¾„Z%dªdšd�d¿dÀœdÁdÂ„Z&d†d†dÃdÄœdÅdÆ„Z'd†dÇd§œdÈdÉ„Z(d�d�dÊdËœdÌdÍ„Z)d�d�dµdËœdÎdÏ„Z*d†d†dÇdÐdÑœdÒdÓ„Z+d†dÔd§œdÕdÖ„Z,dád×dØdØd×dÙœdÚdÛ„Z-dØd‚œdÜdÝ„Z.e/dÞk�røze0e.ƒ ƒ‚W nF e1e2f�yö Z3 z(e4dße3› �ej5dà� e0dƒ‚W Y dZ3[3n
dZ3[30 0 dS )âar  Generate conservative UN R155 V3.2 review mappings with field-weighted matching and candidate diversity.

V3 preserves the V2-compatible ``mappings`` array for Annex 5 Part A candidates,
but adds:

* phrase-aware technical concepts;
* attack-path bridges between a threat consequence and Annex 5 mechanisms;
* negative compatibility gates to reduce obvious false positives;
* R155 CSMS/type-approval relations outside Annex 5;
* explicit review-only output. No mapping is automatically accepted.

The output remains suitable for build_r155_test_pages_v2_2.py because the
existing top-level ``mappings`` structure is retained.
é    )ÚannotationsN)Údefaultdict)ÚPath)ÚAny>   ZvehiclesZtheÚandÚfromZofÚwithZthroughZacrossÚforZintoZsystemsZvehicleZanZwithinÚsystemZcyberZbyÚrelatedZ	regardingZattacksÚtoZcybersecurityZattackÚaÚasÚinZonÚor>!   ZoemZapplicationsZsupportÚmanufacturerZ	functionsÚlossZinsufficientZlostZthirdZpartyZapplicationÚdataZsoftwareZ
componentsZ	componentZcriticalZusedZimpactr   ÚexternalZdigitalZriskZ
managementÚservicesZhostedZrisksÚserviceZfunctionZmanufacturersZsecurityZinformationZusingÚend)zend of supportzend-of-supportzend of lifezend-of-life>   ZobsolescenceZunsupportedZobsoleteZeosZlegacyZeolé   )ÚphrasesÚaliasesÚmin_aliases)z loss of information in the cloudzcloud data losszcloud information loss>   ÚavailabilityÚcloudZbackupr   é   )zback-end serverzbackend serverzback end serverzserver compromise>   Úbackendr   ZinternetÚremoteÚserver)zbackend outagezback-end outagezserver disruptionzservices being disrupted>   r   Úoutager   Ú
disruptionr!   )úsupply chainzthird-partyzthird partyzservice providerztier 1ztier 2>   Z
contractedÚ
dependencyÚsupplierÚ	suppliersÚdependenciesÚprovider)zvehicle takeoverzloss of controlzgain controlzremote controlzunsafe vehicle>   Z
immobilizeZacceleratingZbrakingÚcontrolZsteeringÚdisableZtakeover)zremotely operatezremote operationzremote command>   ZcommandZoperateÚ
telematicsr*   r    )zautonomous drivingzautomated drivingzself drivingzdriving functions>   ÚlidarZ	automatedZadasÚcameraÚ
perceptionZ
autonomous)zsensor inputzsensor inputszsensor spoofingúphysical manipulation>   r-   ZradarÚspoofingr.   ÚsensorÚsensorsZmanipulationr/   )zinfotainment systemzinfotainment unitz	head unit>   zwi-fiZinfotainmentÚwifiÚ	bluetoothÚusbZmedia)zexternal connectivityzexternal interfacezdiagnostic portzobd port>   Zportr4   r5   r,   Z	interfaceZ
diagnosticZobdr6   )zcommunication channelzcommunications channelzsession hijackingzman in the middle>   r1   ZreplayÚmessageZcommunicationZchannelZmessagesZnetwork)zcode injectionzdata injectionzmanipulation of vehicle datazmanipulation of vehicle code>   ZmodifiedZ	overwriteZfirmwareZmodifyZerasureZ	injectionÚcode)znetwork separationznetwork segmentationz#circumvention of network separation>   Z
separationZpivotZgatewayZcircumventionZ	isolationZsegmentation)zunauthorized accesszunauthorised accesszabuse of privilegeszprivilege abuse>   ÚaccessZunauthorisedZbackdoorZ	privilegeZcredentialsZunauthorized)zdata exfiltrationzextract datazinformation disclosureÚeavesdropping>   ÚextractZ
disclosureZexfiltrationZ	espionager:   )zpersonal datazprivacy informationzlocation trackingzvehicle occupantszbehavioral tracking>   Z	occupantsZsurveillanceZtrackingÚprivacyÚ
monitoringZoccupantÚlocation)zuse in botnetszvehicle botnetzdistributed attackzdistributed denial of service>   ZscalableZddosZbotnetsZbotnetZ	endpointsZdistributed)zservice outagezservices outagezmobility serviceszstop functioning>   r   r   ZinterruptionZ	interruptr#   r"   )zshort encryption keyszcryptographic compromisezlong validityzweak cryptography>   ZcryptographicZcryptographyÚkeysZ
encryptionZcertificatesZcertificate)zelectronic hardwarez"replacement of electronic hardwarer0   >   Zreplacementr2   r3   ZhardwareZphysical)zsensitive dataz$loss or compromise of sensitive datazdata breach>   ZtheftZconfidentialityr   r<   Z	sensitive)Zend_of_supportZcloud_information_lossÚbackend_compromiseÚbackend_disruptionÚsupplier_dependencyÚvehicle_controlÚremote_operationÚautonomous_drivingÚsensor_manipulationÚinfotainment_entryÚexternal_connectivityÚcommunication_channelÚcode_data_manipulationÚnetwork_separationÚunauthorized_accessÚdata_exfiltrationÚprivacy_surveillanceÚbotnet_distributed_attackÚservice_availabilityÚcryptographic_weaknessZphysical_sensor_hardwareÚsensitive_data_losszdict[str, dict[str, Any]]ÚCONCEPTSg333333Ó?g¤p=
×£Ð?gìQ¸…ëÑ?g)\�Âõ(Ì?)rH   rI   rJ   rK   rL   ç¸…ëQ¸Î?ç
×£p=
Ç?çš™™™™™É?)rD   rK   rJ   rI   rF   g{®GázÔ?gq=
×£pÍ?ç¸…ëQ¸¾?)rF   rK   rJ   rI   rQ   )rL   rH   r@   rJ   rI   gÃõ(\�ÂÕ?gìQ¸…ëÁ?)rA   r@   rI   rH   )r@   rI   rL   rH   )rM   rL   rI   r@   g{®GázÄ?)r@   rL   rJ   )rG   rC   rE   rO   rP   rM   rN   rB   zdict[str, dict[str, float]]ÚATTACK_PATHS>   )rC   rR   )rO   rR   )rP   rR   )rE   rR   c                 C  s   i | ]}d |› �d |› �“qS )z4.3.© )Ú.0ZnumberrY   rY   ú!build_r155_review_mapping_v3_2.pyÚ
<dictcomp>Ü   ó    r\   é   zR155-CSMS-GOVERNANCEZcsmsz2.3z7.2.2.2(a-d)zHCybersecurity governance, responsibilities and risk management processes)zinsufficient priorityzregulation and oversightzsafety/securityÚ
governance>   Ú	oversightÚ
regulationr   ÚpriorityZ
compliancer_   ))rb   r_   ra   r`   )Úrelation_idÚlayerÚ
referencesÚtitler   r   r   Úrequired_groupszR155-AUTHORITY-COMPETENCEZtype_approvalz5.3.1zYCompetent personnel and uniform evaluation by Approval Authorities and Technical Services)zinsufficient oversightzvehicle authorityztechnical servicezmarket surveillance>   r`   Ú
competencer   Ú	knowledgeÚ
complexityÚ	authorityÚ	technical))r`   rk   rl   rh   ri   rj   zR155-PRE-APPROVAL-TESTING)z5.1.2z5.1.3(d)z7.3.6z7Appropriate and sufficient testing before type approval)z+inadequate testing prior to market approvalztesting prior to approvalzinsufficient testing>   ÚapprovalÚtestÚtestingÚverificationÚmarket))ro   rn   rp   )rm   rq   zR155-SUPPLIER-DEPENDENCIES)z5.1.1(a)z7.2.2.5z7.3.2z8Management of supplier and service-provider dependencies)zvehicle ecosystemr$   zthrough supplierszsupplier dependencies>   r%   r&   Z	ecosystemr'   r(   r)   )rc   rd   re   rf   r   r   r   z"R155-RISK-ASSESSMENT-OTHER-THREATSZvehicle_typez7.3.3z[Exhaustive vehicle-type risk assessment including external systems and other relevant risks)zsocietal disruptionzsystemic riskzshared dependencyzcascading effects>   ZinteractionsZfleetZ	cascadingZsocietalZsystemicr   zR155-MONITORING-RESPONSE)z7.2.2.2(g-h)z7.2.2.4z7.4zGContinual monitoring, response and reporting over the vehicle lifecycle)r=   zincident responseúpost-productionznew cyber threats>   Ú	reportingr=   ZincidentÚdetectÚresponseZ	lifecycle))r=   rt   ru   rs   rr   ztuple[dict[str, Any], ...]ÚREQUIREMENT_RULESzargparse.Namespace)Úreturnc                  C  sp   t jdd�} | jdttdƒd� | jdttdƒd� | jdttd	ƒd� | jd
tdd� | jdtdd� |  ¡ S )Nz Build UN R155 V3 review mappings)Údescriptionz
--registerzdata/threats_register.json)ÚtypeÚdefaultz--annex5zregulatory/r155/annex5.jsonz--outputz)regulatory/r155/r155_threat_mappings.jsonz--min-scorerV   z--max-candidatesé   )ÚargparseÚArgumentParserÚadd_argumentr   ÚfloatÚintÚ
parse_args)ÚparserrY   rY   r[   r�     s    r�   r   zdict[str, Any])Úpathrw   c              
   C  s¢   zt  | jdd�¡}W nn tyJ } ztd| › �ƒ|‚W Y d }~nDd }~0  t jy„ } z"td| › d|› �ƒ|‚W Y d }~n
d }~0 0 t|tƒsžtd| › �ƒ‚|S )Núutf-8©ÚencodingzInput file not found: zInvalid JSON in z: zExpected JSON object in )ÚjsonÚloadsÚ	read_textÚFileNotFoundErrorÚ
ValueErrorZJSONDecodeErrorÚ
isinstanceÚdict)rƒ   r   ÚexcrY   rY   r[   Úload'  s    $,
r�   r   Ústr)Úvaluerw   c                 C  s@   t | pdƒ ¡  dd¡ dd¡}t dd|¡}t dd|¡ ¡ S )NÚ u   â€“ú-u   â€”z[-_/]+ú z\s+)r�   ÚlowerÚreplaceÚreÚsubÚstrip)r‘   ÚtextrY   rY   r[   Ú	normalize3  s     r›   zset[str]c                 C  s   dd„ t  dt| ƒ¡D ƒS )Nc                 S  s$   h | ]}t |ƒd kr|tvr|’qS )r   )ÚlenÚSTOP)rZ   ZwordrY   rY   r[   Ú	<setcomp>:  s   ÿztokens.<locals>.<setcomp>z	[a-z0-9]+)r—   Úfindallr›   ©r‘   rY   rY   r[   Útokens9  s    ÿr¡   c                 C  s0   t | tttfƒr$d dd„ | D ƒ¡S t| p,dƒS )Nr”   c                 s  s   | ]}|rt |ƒV  qd S ©N©r�   )rZ   ÚitemrY   rY   r[   Ú	<genexpr>B  r]   zjoin_field.<locals>.<genexpr>r’   )rŒ   ÚlistÚtupleÚsetÚjoinr�   r    rY   rY   r[   Ú
join_field@  s    rª   g      ð?g333333ë?gffffffî?gÍÌÌÌÌÌì?g      è?)rf   Útitle_nlrx   Úannex5_areaZtermsZsurfacesZtagszdict[str, float]ÚCORE_FIELD_WEIGHTSgffffffÖ?g      Ð?)ÚcategoryZprimary_domainZsecondary_domainsZaffectsÚCATEGORY_FIELD_WEIGHTSç{®Gáz´?g¸…ëQ¸®?ç{®Gáz¤?)Zdomain_rationaleZimpact_rationaleZlikelihood_rationaleZrisk_rationalerb   ÚCONTEXT_FIELD_WEIGHTS©rf   r«   rx   zdict[str, dict[str, str]])Úthreatrw   c                   s6   ‡ fdd„t D ƒ‡ fdd„tD ƒ‡ fdd„tD ƒdœS )Nc                   s2   i | ]*}t ˆ  |d ¡ƒ ¡ r|t ˆ  |d ¡ƒ“qS ©r’   ©rª   Úgetr™   ©rZ   Úfield©r´   rY   r[   r\   i  s   þz!threat_fields.<locals>.<dictcomp>c                   s2   i | ]*}t ˆ  |d ¡ƒ ¡ r|t ˆ  |d ¡ƒ“qS rµ   r¶   r¸   rº   rY   r[   r\   n  s   þc                   s2   i | ]*}t ˆ  |d ¡ƒ ¡ r|t ˆ  |d ¡ƒ“qS rµ   r¶   r¸   rº   rY   r[   r\   s  s   þ)Úcorer®   Úcontext)r­   r¯   r²   rº   rY   rº   r[   Úthreat_fieldsg  s    
þ
þ
þõr½   zdict[str, str])Úfieldsrw   c                 C  s   d  dd„ |  ¡ D ƒ¡S )Nr”   c                 s  s   | ]}|r|V  qd S r¢   rY   ©rZ   r‘   rY   rY   r[   r¥   |  r]   z!combine_fields.<locals>.<genexpr>)r©   Úvalues©r¾   rY   rY   r[   Úcombine_fields{  s    rÂ   )Únoderw   c              
   C  s<   d  dd„ |  dd¡|  dd¡|  dd¡|  dd¡fD ƒ¡S )	Nr”   c                 s  s   | ]}|rt |ƒV  qd S r¢   r£   r¿   rY   rY   r[   r¥   €  s   ýznode_text.<locals>.<genexpr>Zlabelr’   Zcategory_titlerx   Zexample)r©   r·   )rÃ   rY   rY   r[   Ú	node_text  s
    
þÿrÄ   z"tuple[bool, str | None, list[str]])rš   Úrulerw   c                 C  st   t | ƒ}| dd¡D ]"}t |ƒ|v rdt|ƒg f  S qtt|ƒt| dtƒ ¡ƒ@ ƒ}t|ƒt| dd¡ƒkd |fS )Nr   rY   Tr   r   r   )r›   r·   r�   Úsortedr¡   r¨   rœ   r€   )rš   rÅ   Z
normalizedÚphraseZ
alias_hitsrY   rY   r[   Úrule_match_detailsˆ  s    rÈ   Úboolc                 C  s   t | |ƒd S )Nr   )rÈ   )rš   rÅ   rY   rY   r[   Ú	rule_hits‘  s    rÊ   )rš   rw   c                   s   ‡ fdd„t  ¡ D ƒS )Nc                   s   h | ]\}}t ˆ |ƒr|’qS rY   )rÊ   )rZ   ÚnamerÅ   ©rš   rY   r[   rž   –  r]   zconcept_hits.<locals>.<setcomp>)rS   ÚitemsrÌ   rY   rÌ   r[   Úconcept_hits•  s    rÎ   zdict[str, list[dict[str, Any]]])r¾   Úweightsrw   c           
   	   C  sf   t tƒ}|  ¡ D ]L\}}t ¡ D ]:\}}t||ƒ\}}}	|s>q ||  ||| ||	dœ¡ q qt|ƒS )N)Úsource_fieldÚfield_weightÚmatched_phraseÚmatched_aliases)r   r¦   rÍ   rS   rÈ   Úappendr�   )
r¾   rÏ   Úevidencer¹   rš   rË   rÅ   ÚmatchedrÇ   r   rY   rY   r[   Úfield_concept_evidence™  s    ür×   z,tuple[float, set[str], list[dict[str, Any]]])Úthreat_field_maprÏ   Únode_tokensrw   c           
      C  sv   d}t ƒ }g }|  ¡ D ]T\}}t|ƒ|@ t }|s4q|| }	||	t|ƒ 7 }||O }| ||	t|ƒdœ¡ q|||fS )Nç        )rÐ   rÑ   Úmatched_terms)r¨   rÍ   r¡   ÚWEAK_TOKENSrœ   rÔ   rÆ   )
rØ   rÏ   rÙ   ÚtotalrÖ   Ú
provenancer¹   rš   Z
field_hitsZweightrY   rY   r[   Úweighted_overlap¬  s     ý
rß   z!tuple[bool, list[dict[str, Any]]])r´   rÅ   rw   c                   sÂ  t | ƒ‰ d}‡ fdd„|D ƒ}ˆ d }tt|ƒƒ‰| dd¡D ]Z}dd„ |D ƒ}tˆƒ}t‡fd	d
„|D ƒƒ}t|dd„ |D ƒ@ ƒ}	|s>|	s>dg f  S q>g }
| ¡ D ].\}}t||ƒ\}}}|r¦|
 	|||dœ¡ q¦|
râd|
fS t
ƒ }| ¡ D ]"}|t|ƒt
| dt
ƒ ¡ƒ@ O }qð|�s"dg fS t
ƒ }g }| ¡ D ]H\}}t|ƒt
| dt
ƒ ¡ƒ@ }|�r4||O }| 	|dt|ƒdœ¡ �q4t||B ƒt| dd¡ƒk}|�s¨dg fS dddt|ƒdœg| fS )zJRequire primary scenario evidence; category can support but not originate.r³   c                   s&   i | ]}|ˆ d  v r|ˆ d  | “qS ©r»   rY   )rZ   rË   rÁ   rY   r[   r\   Ç  r]   z*requirement_rule_match.<locals>.<dictcomp>r®   rg   rY   c                 S  s   h | ]}t |ƒ’qS rY   )r›   ©rZ   ZtermrY   rY   r[   rž   Î  r]   z)requirement_rule_match.<locals>.<setcomp>c                 3  s   | ]}d |v r|ˆ v V  qdS )r”   NrY   rá   )Úprimary_textrY   r[   r¥   Ð  r]   z)requirement_rule_match.<locals>.<genexpr>c                 S  s   h | ]}d |vr|’qS )r”   rY   rá   rY   rY   r[   rž   Ñ  r]   F)rÐ   rÒ   rÓ   Tr   Nr   r   Ztitle_or_description)r½   r›   rÂ   r·   r¡   ÚanyrÉ   rÍ   rÈ   rÔ   r¨   rÀ   rÆ   rœ   r€   )r´   rÅ   Zprimary_namesZprimaryZ	secondaryÚgroupZnormalized_groupZprimary_tokensZ
phrase_hitZ	token_hitrÕ   r¹   rš   rÖ   rÇ   r   Zprimary_aliasesZcategory_aliasesZsupport_evidenceÚhitsZenoughrY   )r¾   râ   r[   Úrequirement_rule_matchÃ  sR     ýüræ   z
str | Nonec                   s.   t |  d¡pdƒ‰ t‡ fdd„t ¡ D ƒd ƒS )Nr¬   r’   c                 3  s   | ]\}}|ˆ v r|V  qd S r¢   rY   )rZ   Zarear®   ©ÚrawrY   r[   r¥   ö  r]   z explicit_area.<locals>.<genexpr>)r�   r·   ÚnextÚAREA_TO_CATEGORYrÍ   rº   rY   rç   r[   Úexplicit_areaô  s    rë   ztuple[float, list[str]])Úthreat_conceptsÚnode_conceptsrw   c                 C  s~   d}g }t | ƒD ]d}t |ƒD ]V}t |i ¡ |d¡}||krR|}|› d|› �g}q|r||kr| |› d|› �¡ qq||fS )NrÚ   z->)rÆ   rX   r·   rÔ   )rì   rí   ÚscoreÚpathsÚsourceÚtargetZboostrY   rY   r[   Úbest_attack_pathù  s    rò   c                   s   t ‡ fdd„| D ƒƒS )Nc                 3  s$   | ]}ˆ D ]}||ft v V  q
qd S r¢   )ÚINCOMPATIBLE_CONCEPTS)rZ   ÚleftÚright©rí   rY   r[   r¥     r]   zincompatible.<locals>.<genexpr>)rã   )rì   rí   rY   rö   r[   Úincompatible  s    r÷   zItuple[float, list[str], list[str], list[str], str, dict[str, Any]] | None)r´   rÃ   Úexplicitrw   c           "        sî  t | ƒ‰ tˆ d ƒ}tˆ d ƒ}tˆ d ƒ}t|ƒ}t|ƒ}tˆ d tƒ}tˆ d tƒ}	‡ fdd„tD ƒ}
t|
dd„ |
D ƒƒ}t|ƒ}t|ƒ}t|	ƒ}t	|ƒ}||@ }t
ˆ d t|ƒ\}}}t
ˆ d t|ƒ\}}}t
ˆ d t|ƒ\}}}||B |B }|||	|| | t|| | dƒdœ}|�rH| d	¡|k�rHd
t|ƒt|ƒg d|fS t||ƒ\}}t|�p`||ƒ�r‚|�s‚|dk �r‚|�s‚d S |�ròdtddt|ƒ ƒ }|tdd| ƒ7 }|tdd| ƒ7 }|tdd| ƒ7 }t|dƒt|ƒt|ƒ|d|fS |�rPd| }|tdd| ƒ7 }|tdd| ƒ7 }|tdd| ƒ7 }t|dƒt|ƒg |d|fS t|ƒ}t|ƒdk �sp|dk �rtd S t|ƒ|B } | �r–t|ƒt| ƒ nd }!d!|! td"d| ƒ }|tdd| ƒ7 }|tdd| ƒ7 }t|d#ƒt|ƒg g d$|fS )%Nr»   r®   r¼   c                   s&   i | ]}|ˆ d  v r|ˆ d  | “qS rà   rY   r¸   rÁ   rY   r[   r\     s   þz#candidate_score.<locals>.<dictcomp>c                 S  s   i | ]}|t | “qS rY   )r­   r¸   rY   rY   r[   r\     r]   é   )Úattack_source_conceptsÚcore_conceptsZsupporting_category_conceptsZterm_evidenceZweighted_term_scoreÚcategory_referencegffffffæ?Úexplicit_annex5_areag      ø?g¸…ëQ¸Þ?rU   gìQ¸…ë±?gš™™™™™¹?gš™™™™™™?r±   g¸…ëQ¸Ž?g{®Gáz”?g{®Gáz„?gq=
×£pí?Zshared_core_conceptgR¸…ëQØ?r°   g¸…ëQ¸ž?g{®Gázt?g)\�Âõ(ì?Zcore_attack_path_bridger   r   rÚ   rW   rT   g
×£p=
ç?Zweighted_core_lexical_seed)r½   rÂ   rÄ   r¡   r×   r­   r¯   ÚATTACK_SOURCE_FIELDSr¨   rÎ   rß   r²   Úroundr·   rÆ   rò   r÷   Úminrœ   )"r´   rÃ   rø   Zcore_rawZcategory_rawZcontext_rawZnode_rawrÙ   Zcore_concept_evidenceZcategory_concept_evidenceZattack_source_fieldsZattack_source_evidencerú   rû   Zcategory_conceptsrí   Zshared_core_conceptsZcore_weightZ
core_termsZcore_term_evidenceZcategory_weightZcategory_termsZcategory_term_evidenceZcontext_weightZcontext_termsZcontext_term_evidenceÚoverlaprÞ   Z
path_scorerï   rî   Zcore_fields_with_termsÚunionZjaccardrY   rÁ   r[   Úcandidate_score  s|    
þþ
ÿ

ÿ

ÿ

û(r  zlist[dict[str, Any]]c                 C  s‚   g }t D ]t}t| |ƒ\}}|s q| |d › d| d › �| d |  dd¡d|d t|d ƒ|d d	d
|dd d d d dœdœ¡ q|S )Nrc   r“   Úidrf   r’   úUN-R155rd   re   Úreviewr   zuCandidate generated from title/description evidence, optionally supported by category fields; expert review required.©ZdecisionZreviewed_byZreviewed_atZnote)rc   Ú	threat_idÚthreat_titleÚregulation_idrd   re   rf   ÚstatusÚrelationshipÚfield_evidenceÚ	rationaler  )rv   ræ   rÔ   r·   r¦   )r´   Ú	relationsrÅ   rÖ   rÕ   rY   rY   r[   Úrequirement_relations^  s(    

ô
r  zlist[tuple[Any, ...]]r€   )Ú
candidatesÚlimitÚmax_per_attack_pathrw   c           	      C  sª   g }g }t tƒ}| D ]l}|d }|r,|d nd|d › �}|| |krR| |¡ q| |¡ ||  d7  < t|ƒ|kr|  S q|D ]}| |¡ t|ƒ|kr† q¦q†|S )zKSelect high scoring candidates without one attack path monopolising output.é   r   zbasis:r{   r   )r   r€   rÔ   rœ   )	r  r  r  ÚselectedZdeferredZpath_countsÚ	candidaterï   ZfamilyrY   rY   r[   Úselect_diverse_candidatesu  s$    



r  c                    sZ  t ƒ } t| jƒ}t| jƒ}| dg ¡}dd„ | dg ¡D ƒ}t|tƒrJ|sRtdƒ‚g }g }d}i ‰ i ‰|D �]‚}t|tƒrj| d¡s†qjt	|d ƒ}	t
|ƒ}
g }|D ]V}t|||
ƒ}|d u rÄ|d7 }q¢|\}}}}}}|| jk ràq¢| |||||||f¡ q¢|jd	d
„ dd� t|| jdd�}t|ƒˆ |	< |D ]¢\}}}}}}}| d|	› dt	|d ƒ dd¡› �|	| dd¡|d |d | d¡ddt|dƒ|||||| dg ¡|dk�r¶d|
› d�ndd d d d dœdœ¡ �q,t|ƒ}t|ƒˆ|	< | |¡ qjd d„ |D ƒ}g }|D ]P}	ˆ  |	d¡}ˆ |	d¡}|�r.d!}n|�r:d"}nd#}| |	|||dd$œ¡ �qd%d&d'd(dd)t	| jƒt|ƒd*œt	| jƒt|ƒd+œd,d,ddttƒdttƒttƒttƒttƒdd-œt|ƒt|ƒtd.d/„ ˆ  ¡ D ƒƒtd0d/„ ˆ ¡ D ƒƒ‡ ‡fd1d„|D ƒ|d2œ|||d3œ}| jjj ddd4� | jj!t"j#|dd,d5�d6 d7d8� t$t"j#|d9 dd:�ƒ dS );NÚthreatsc                 S  s(   g | ] }t |tƒr| d ¡dkr|‘qS )ÚpartÚA)rŒ   r�   r·   )rZ   rÃ   rY   rY   r[   Ú
<listcomp>–  r]   zmain.<locals>.<listcomp>Únodesz1Threat register or Annex 5 Part A nodes are emptyr   r  r   c                 S  s(   | d t | d ƒt | d ƒt | d ƒfS )Nr   r  rù   r   )rœ   )r¤   rY   rY   r[   Ú<lambda>±  r]   zmain.<locals>.<lambda>T)ÚkeyÚreverser   )r  zR155-r“   Z	referenceÚ.Ú_rf   r’   rü   r   r  rù   Zrelated_mitigationsrý   z3Threat register explicitly identifies Annex 5 area z�Candidate generated from shared technical concepts, an attack-path bridge, or multiple non-generic terms; expert review required.r  )Z
mapping_idr  r	  Zregulatory_node_idZregulatory_referenceZregulatory_categoryr  r  rî   ÚbasisrÛ   Zmatched_conceptsZattack_pathsr  Zmitigation_nodesr  r  c                 S  s,   g | ]$}t |tƒr| d ¡rt|d  ƒ‘qS )r  )rŒ   r�   r·   r�   )rZ   ÚtrY   rY   r[   r  Ô  r]   Zannex5_candidates_generatedZ2r155_requirement_relation_without_annex5_candidateZno_direct_mapping_generated)r  ÚclassificationZannex5_candidate_countÚr155_requirement_relation_countÚexpert_review_requiredz2.2zNCRC-R155-REVIEW-3.2r  zUN-R155-ANNEX-5z¤field-weighted scenario matching with title/description attack origins, attack-path diversity limits, compatibility gates, and hard-gated R155 requirement relations)ÚfileZthreat_count)r'  Zpart_a_reference_countF)Zaccepted_createdZlegal_compliance_inferencer&  Z&recommended_independent_expert_reviewsZ#weak_tokens_cannot_create_candidateZminimum_non_generic_overlapZ!core_fields_can_create_candidatesZcategory_fields_support_onlyZcontext_fields_rank_onlyZattack_path_source_fieldsZ*max_candidates_per_attack_path_before_fillc                 s  s   | ]}|d kV  qdS ©r   NrY   r¿   rY   rY   r[   r¥      r]   zmain.<locals>.<genexpr>c                 s  s   | ]}|d kV  qdS r(  rY   r¿   rY   rY   r[   r¥     r]   c                   s(   g | ] }ˆ   |d ¡sˆ  |d ¡s|‘qS )r   )r·   )rZ   Útid©ZcountsZrelation_countsrY   r[   r    s   )Zannex5_mapping_countr%  Zthreats_with_annex5_candidatesZ"threats_with_requirement_relationsZthreats_without_any_candidateZcandidate_pairs_rejected)Zschema_versionZmapping_set_idr
  Zannex_idr  ÚmethodZsource_threat_registerZregulatory_sourceZrulesÚsummaryZmappingsZregulatory_relationsZthreat_assessments)ÚparentsÚexist_ok)ÚindentZensure_asciiÚ
r„   r…   r,  )r/  )%r�   r�   ÚregisterZannex5r·   rŒ   r¦   r‹   r�   r�   rë   r  Z	min_scorerÔ   Úsortr  Zmax_candidatesrœ   r–   rÿ   r  ÚextendrÆ   rÜ   r­   r¯   r²   rþ   ÚsumrÀ   ÚoutputÚparentÚmkdirZ
write_textr‡   ÚdumpsÚprint)Zconfigr1  Zannexr  r  Zrowsr  Zrejected_pairsr´   r)  rø   r  rÃ   Úresultrî   r  Zconceptsrï   r"  rÞ   r  Zcurrent_relationsZ
threat_idsZassessmentsZannex_countZrequirement_countr$  r5  rY   r*  r[   Úmain‘  sà    



þ

ÿëû	õÿø
Þ% r;  Ú__main__zERROR: )r'  )r   )6Ú__doc__Z
__future__r   r|   r‡   r—   ÚsysÚcollectionsr   Úpathlibr   Útypingr   r�   rÜ   rS   Ú__annotations__rX   ró   Úrangerê   rv   r�   r�   r›   r¡   rª   r­   r¯   r²   rþ   r½   rÂ   rÄ   rÈ   rÊ   rÎ   r×   rß   ræ   rë   rò   r÷   r  r  r  r;  Ú__name__Ú
SystemExitr‹   ÚOSErrorrŽ   r9  ÚstderrrY   rY   rY   r[   Ú<module>   sî  ýýýýýýýýýýýýýýýýýýýýýý–uûûûûüüüýÑ7øøøù
ù
øÏ>
ù
üû
		1S ý 
