""" Ownership Graph Engine Tools | AgenThink SME Credit Intelligence System Responsibility: Build and analyse beneficial ownership graphs for Kuwaiti SMEs. Detects circular ownership, shell company chains, and UBO identification. Compliance: FATF Recommendation 24 (Beneficial Ownership Transparency). CBK AML/CFT Guidelines 2022. """ import logging from typing import List, Optional logger = logging.getLogger(__name__) class OwnershipGraphEngine: """Graph-based beneficial ownership analysis engine.""" def __init__(self): logger.info("[GraphEngine] Ownership Graph Engine initialised.") def build( self, cr_number: str, moci_owners: List[dict], kse_shareholders: List[dict] = None, max_depth: int = 5, ) -> dict: """ Build a beneficial ownership graph. Args: cr_number: Root company CR number. moci_owners: List of direct owners from MOCI. kse_shareholders: List of KSE-disclosed shareholders. max_depth: Maximum ownership chain depth to traverse. Returns: dict with ownership tree, UBOs, depth, and circular ownership flag. """ kse_shareholders = kse_shareholders or [] all_owners = moci_owners + kse_shareholders # Build adjacency representation graph = {cr_number: [o.get("id", o.get("name", "UNKNOWN")) for o in all_owners]} ubos = [] visited = set() circular = False depth = 0 def traverse(node, current_depth, path): nonlocal circular, depth if current_depth > max_depth: return if node in visited: circular = True return visited.add(node) depth = max(depth, current_depth) children = graph.get(node, []) if not children: ubos.append(node) # Leaf node = UBO for child in children: traverse(child, current_depth + 1, path + [child]) traverse(cr_number, 0, [cr_number]) return { "root_cr": cr_number, "graph": graph, "ubos": list(set(ubos)), "depth": depth, "circular_ownership": circular, "total_entities": len(visited), } def detect_shell_indicators(self, graph_result: dict) -> List[str]: """Identify shell company indicators from the ownership graph.""" indicators = [] if graph_result.get("depth", 0) > 3: indicators.append("DEEP_CHAIN") if graph_result.get("circular_ownership"): indicators.append("CIRCULAR_OWNERSHIP") if graph_result.get("total_entities", 0) > 10: indicators.append("COMPLEX_STRUCTURE") if not graph_result.get("ubos"): indicators.append("NO_UBO") return indicators