packages feed

canontra-0.4.0.0: src/Canontra/Analysis/WholeRepoGraph.hs

{- |
Module      : Canontra.Analysis.WholeRepoGraph
Description : Cross-module global call graph and inter-procedural data-flow synthesizer.

This module resolves symbol references across polyglot file boundaries to construct
a unified repository-level Call Graph (F_WCG) and Inter-Procedural Data-Flow Graph (F_WDF).
It handles cross-module edges, circular import/call cycles using Tarjan's SCC algorithm,
and tracks parameter-to-argument and return-value data flow propagation.
-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
module Canontra.Analysis.WholeRepoGraph
  ( DeclKind (..)
  , GlobalSymbol (..)
  , WholeRepoCallEdge (..)
  , WholeRepoCallGraph (..)
  , InterProceduralDataFlowEdge (..)
  , WholeRepoDataFlowGraph (..)
  , CallFlowSite (..)
  , FileGraphSummary (..)
  , extractFileGraphSummary
  , buildWholeRepoCallGraph
  , buildWholeRepoCallGraphFromSummaries
  , buildWholeRepoDataFlow
  , buildWholeRepoDataFlowFromSummaries
  , toCSRCallGraph
  , toCSRDataFlowGraph
  , buildCSRCallGraph
  , buildCSRDataFlow
  , formatWholeRepoCallGraph
  , formatWholeRepoDataFlow
  , findWholeRepoSCCs
  , findCrossModuleEdges
  , findDeadSymbols
  , filePathToModuleName
  , incrementalUpdateWholeRepoCallGraph
  , incrementalUpdateWholeRepoDataFlow
  , incrementalUpdateWholeRepoGraphs
  ) where

import Control.DeepSeq (NFData)
import Data.Bits ((.|.))
import Data.List (foldl', sortBy)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Vector as V
import Data.Word (Word32)
import GHC.Generics (Generic)
import System.FilePath (dropExtension, normalise, splitDirectories, takeDirectory, takeExtension, takeFileName)

import Canontra.Analysis.CSRGraph
  ( CSRGraph (..)
  , buildCSRGraph
  , csrOutDegree
  , flagCallAsync
  , flagCallSync
  , flagCrossModule
  , flagDataFlowRet
  , flagDataFlowUse
  , flagNone
  , tarjanSCC
  , transposeCSR
  )

import Canontra.Analysis.CallGraph (CallEdge (..), CallGraph (..), CalleeTarget (..), CallerNode (..), buildCallGraph)
import Canontra.Canonical.Serialize (canonicalizeDeclaration, canonicalizeWholeRepoCallGraph, canonicalizeWholeRepoDataFlow)
import Canontra.Fingerprint.Source (hashBytes)
import Canontra.IR.Declaration
import Canontra.IR.Dependency (ImportDecl (..), ImportTarget (..))
import Canontra.IR.Expression
  ( CompFor (..)
  , Expr (..)
  , FStringPart (..)
  , MatchCase (..)
  , SelectCase (..)
  , Stmt (..)
  )
import Canontra.IR.Program (Module (..), Program (..))
import Canontra.Types
  ( DeclKind (..)
  , Fingerprint (..)
  , GlobalSymbol (..)
  , InterProceduralDataFlowEdge (..)
  , WholeRepoCallEdge (..)
  , WholeRepoCallGraph (..)
  , WholeRepoDataFlowGraph (..)
  )

-- | Convert a source file path into a canonical dotted module name.
-- E.g. "auth/jwt.py" -> "auth.jwt", "src/core/math.rs" -> "src.core.math".
filePathToModuleName :: FilePath -> Text
filePathToModuleName rawPath =
  let norm = map (\c -> if c == '\\' then '/' else c) (normalise rawPath)
      noExt = dropExtension norm
      dirs = splitDirectories noExt
      filteredDirs = filter (\d -> d /= "." && d /= "/" && d /= "\\") dirs
      effectiveDirs = case reverse filteredDirs of
        ("__init__" : rest) -> reverse rest
        other               -> reverse other
  in T.intercalate "." (map T.pack effectiveDirs)

extractProgramSymbols :: FilePath -> Program -> [GlobalSymbol]
extractProgramSymbols fp (Program mods _) =
  let pathMod = filePathToModuleName fp
  in concatMap (extractModuleSymbols fp pathMod) mods

extractModuleSymbols :: FilePath -> Text -> Module -> [GlobalSymbol]
extractModuleSymbols fp pathMod (Module mName _ decls _) =
  let effectiveMod = effectiveModuleName fp pathMod mName
  in concatMap (declToGlobalSymbols fp effectiveMod) decls

-- | Compute the canonical effective module namespace for a source file.
-- For Go files, merges sibling files within the same directory that share a
-- @package@ declaration into one unified package key (avoiding false isolation
-- or blind cross-package fallback).
effectiveModuleName :: FilePath -> Text -> Text -> Text
effectiveModuleName fp pathMod mName
  | takeExtension fp == ".go" =
      let norm = map (\c -> if c == '\\' then '/' else c) (normalise fp)
          dirRaw = takeDirectory norm
          dirSegs = filter (\d -> d /= "." && d /= "/" && d /= "" && d /= "\\") (splitDirectories dirRaw)
          cleanPkg
            | T.null mName || mName == T.pack fp || T.isInfixOf "/" mName || T.isInfixOf "\\" mName =
                case reverse dirSegs of
                  (d:_) -> T.pack d
                  []    -> pathMod
            | otherwise = mName
      in case reverse dirSegs of
           [] -> cleanPkg
           (lastDir : _)
             | T.pack lastDir == cleanPkg -> T.intercalate "." (map T.pack dirSegs)
             | otherwise                  -> T.intercalate "." (map T.pack dirSegs) <> "." <> cleanPkg
  | otherwise =
      let isPathLike t =
            T.null t
              || t == "main"
              || t == T.pack fp
              || T.isInfixOf "/" t
              || T.isInfixOf "\\" t
              || any (`T.isSuffixOf` t) [".py", ".pyi", ".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".go", ".rs"]
      in if isPathLike mName then pathMod else mName

isBarrelFile :: FilePath -> Bool
isBarrelFile fp =
  let base = takeFileName fp
  in base `elem`
       [ "__init__.py"
       , "index.ts", "index.tsx", "index.js", "index.mjs", "index.cjs"
       , "mod.rs", "lib.rs"
       ]

barrelParentModule :: FilePath -> Maybe Text
barrelParentModule fp
  | not (isBarrelFile fp) = Nothing
  | otherwise =
      let norm = map (\c -> if c == '\\' then '/' else c) (normalise fp)
          dirRaw = takeDirectory norm
          dirSegs = filter (\d -> d /= "." && d /= "/" && d /= "" && d /= "\\") (splitDirectories dirRaw)
      in if null dirSegs then Nothing else Just (T.intercalate "." (map T.pack dirSegs))

-- | Normalize an import specifier (relative JS/TS/Python path or Rust crate path)
-- relative to the importing file's directory into a canonical dotted module name.
normalizeImportSpecifier :: FilePath -> Text -> Text
normalizeImportSpecifier curPath rawSpec
  | T.isPrefixOf "./" rawSpec || T.isPrefixOf "../" rawSpec =
      let normCur = map (\c -> if c == '\\' then '/' else c) (normalise curPath)
          curDir = takeDirectory normCur
          curSegs = filter (\d -> d /= "." && d /= "" && d /= "/") (splitDirectories curDir)
          rawNoExt = stripSourceExt rawSpec
          specSegs = T.splitOn "/" rawNoExt
          resolvedSegs = foldl' stepSeg curSegs specSegs
          trimmed = case reverse resolvedSegs of
            ("index" : rest) -> reverse rest
            other            -> reverse other
      in T.intercalate "." (map T.pack trimmed)
  | T.isPrefixOf "." rawSpec =
      let dots = T.length (T.takeWhile (== '.') rawSpec)
          restSpec = T.drop dots rawSpec
          normCur = map (\c -> if c == '\\' then '/' else c) (normalise curPath)
          curDir = takeDirectory normCur
          curSegs = filter (\d -> d /= "." && d /= "" && d /= "/") (splitDirectories curDir)
          upCount = max 0 (dots - 1)
          baseSegs = take (max 0 (length curSegs - upCount)) curSegs
          extraSegs = if T.null restSpec then [] else map T.unpack (T.splitOn "." restSpec)
      in T.intercalate "." (map T.pack (baseSegs ++ extraSegs))
  | T.isPrefixOf "crate::" rawSpec =
      T.replace "::" "." (T.drop 7 rawSpec)
  | T.isInfixOf "::" rawSpec =
      T.replace "::" "." rawSpec
  | "/" `T.isInfixOf` rawSpec =
      let noExt = stripSourceExt rawSpec
          segs = filter (not . T.null) (T.splitOn "/" noExt)
          trimmed = case reverse segs of
            ("index" : rest) -> reverse rest
            other            -> reverse other
      in T.intercalate "." trimmed
  | otherwise = rawSpec
  where
    stepSeg segs s
      | s == "."  || T.null s = segs
      | s == ".."             = if null segs then [] else init segs
      | otherwise             = segs ++ [T.unpack s]

    stripSourceExt t =
      case filter (`T.isSuffixOf` t) [".py", ".pyi", ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".go", ".rs"] of
        (ext : _) -> T.dropEnd (T.length ext) t
        []        -> t

-- | Collect normalized imported module names and barrel re-exports from a program's modules.
extractImportsAndBarrels :: FilePath -> Program -> ([Text], [(Text, Text, Text)])
extractImportsAndBarrels fp (Program mods _) =
  let allImps = concatMap modImports mods
      impMods = Set.toAscList $ Set.fromList
        [ normalizeImportSpecifier fp m
        | imp <- allImps
        , let m = case imp of
                    ImportModule p _ -> p
                    ImportFrom p _   -> p
        , not (T.null m)
        ]
      reExps =
        [ item
        | imp <- allImps
        , item <- impToReExports imp
        ]
  in (impMods, reExps)
  where
    impToReExports = \case
      ImportFrom srcMod ImportAll ->
        [("*", normalizeImportSpecifier fp srcMod, "*")]
      ImportFrom srcMod (ImportSymbols pairs)
        | isBarrelFile fp || isJSOrRust ->
            [ (maybe sym id mAlias, normalizeImportSpecifier fp srcMod, sym)
            | (sym, mAlias) <- pairs
            ]
      ImportModule srcMod (Just alias)
        | isBarrelFile fp ->
            [(alias, normalizeImportSpecifier fp srcMod, alias)]
      _ -> []
    isJSOrRust =
      takeExtension fp `elem` [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".rs"]

declToGlobalSymbols :: FilePath -> Text -> Declaration -> [GlobalSymbol]
declToGlobalSymbols fp modName decl =
  let f2 = hashBytes (canonicalizeDeclaration decl)
  in case decl of
    DeclFunction fn ->
      [GlobalSymbol fp modName (fnName fn) KindFunction f2]

    DeclClass cls ->
      let classSym = GlobalSymbol fp modName (clsName cls) KindClass f2
          methodSyms =
            [ GlobalSymbol fp modName (clsName cls <> "." <> fnName m) KindMethod
                (hashBytes (canonicalizeDeclaration (DeclFunction m)))
            | m <- clsMethods cls
            ]
      in classSym : methodSyms

    DeclStruct st ->
      let structSym = GlobalSymbol fp modName (stName st) KindStruct f2
          methodSyms =
            [ GlobalSymbol fp modName (stName st <> "." <> fnName m) KindMethod
                (hashBytes (canonicalizeDeclaration (DeclFunction m)))
            | m <- stMethods st
            ]
      in structSym : methodSyms

    DeclTrait tr ->
      let traitSym = GlobalSymbol fp modName (trName tr) KindTrait f2
          methodSyms =
            [ GlobalSymbol fp modName (trName tr <> "." <> fnName m) KindMethod
                (hashBytes (canonicalizeDeclaration (DeclFunction m)))
            | m <- trMethods tr
            ]
      in traitSym : methodSyms

    DeclInterface iface ->
      let ifaceSym = GlobalSymbol fp modName (ifName iface) KindInterface f2
          methodSyms =
            [ GlobalSymbol fp modName (ifName iface <> "." <> fnName m) KindMethod
                (hashBytes (canonicalizeDeclaration (DeclFunction m)))
            | m <- ifMethods iface
            ]
      in ifaceSym : methodSyms

    DeclReceiver rc fn ->
      [GlobalSymbol fp modName (rcTypeName rc <> "." <> fnName fn) KindMethod f2]

    DeclImpl imp ->
      [ GlobalSymbol fp modName (impTarget imp <> "." <> fnName m) KindMethod
          (hashBytes (canonicalizeDeclaration (DeclFunction m)))
      | m <- impMethods imp
      ]

    DeclVariable name _ ->
      [GlobalSymbol fp modName name KindVariable f2]

    DeclTypeAlias name _ ->
      [GlobalSymbol fp modName name KindTypeAlias f2]

-- | Barrel re-export registry: maps canonical module names (including parent barrel aliases
-- like @"pkg"@ for @"pkg/index.ts"@) to explicit re-exported symbols and wildcard source modules.
type BarrelMap = Map Text (Map Text (Text, Text), [Text])

-- | Multi-index symbol lookup table for robust, import-scoped cross-module resolution.
data SymbolIndices = SymbolIndices
  { idxByModAndName  :: !(Map (Text, Text) GlobalSymbol)
  , idxByPathAndName :: !(Map (FilePath, Text) GlobalSymbol)
  , idxByNameOnly    :: !(Map Text [GlobalSymbol])
  , idxBarrelMap     :: !BarrelMap
  }

buildBarrelMapFromSummaries :: [FileGraphSummary] -> BarrelMap
buildBarrelMapFromSummaries summaries =
  foldl' addSummary Map.empty summaries
  where
    addSummary acc s =
      let fp = fgsFilePath s
          modName = fgsModuleName s
          keys = modName : maybe [] pure (barrelParentModule fp)
          (symMap, wilds) = foldl' splitReExp (Map.empty, []) (fgsReExports s)
      in if Map.null symMap && null wilds
           then acc
           else foldl' (\m k -> Map.insertWith mergeBarrel k (symMap, wilds) m) acc keys

    splitReExp (sm, ws) (expName, srcM, origName)
      | expName == "*" = (sm, srcM : ws)
      | otherwise      = (Map.insert expName (srcM, origName) sm, ws)

    mergeBarrel (s1, w1) (s2, w2) = (Map.union s1 s2, w1 ++ w2)

buildSymbolIndices :: [GlobalSymbol] -> BarrelMap -> SymbolIndices
buildSymbolIndices syms barrelMap =
  let byModName = Map.fromList [((symModule s, symDeclName s), s) | s <- syms]
      byPathName = Map.fromList [((symFilePath s, symDeclName s), s) | s <- syms]
      byName = Map.fromListWith (++) [(symDeclName s, [s]) | s <- syms]
  in SymbolIndices byModName byPathName byName barrelMap

followBarrel
  :: SymbolIndices
  -> Set (Text, Text)
  -> Int
  -> Text
  -> Text
  -> Maybe GlobalSymbol
followBarrel _ _ depth _ _ | depth >= 16 = Nothing
followBarrel indices visited depth modName symName
  | Set.member (modName, symName) visited = Nothing
  | Just s <- Map.lookup (modName, symName) (idxByModAndName indices) = Just s
  | otherwise =
      let visited' = Set.insert (modName, symName) visited
      in case Map.lookup modName (idxBarrelMap indices) of
           Nothing -> Nothing
           Just (explicitMap, wildMods) ->
             case Map.lookup symName explicitMap of
               Just (srcMod, origSym) ->
                 followBarrel indices visited' (depth + 1) srcMod origSym
               Nothing ->
                 firstJust [followBarrel indices visited' (depth + 1) wm symName | wm <- wildMods]
  where
    firstJust []           = Nothing
    firstJust (Just x : _) = Just x
    firstJust (Nothing : xs) = firstJust xs

-- | Resolve an invocation target against the multi-index, import-scoped symbol table
-- and barrel re-export registry. Avoids blind cross-package fallback collisions.
resolveTarget
  :: SymbolIndices
  -> Text        -- ^ Current module name
  -> FilePath    -- ^ Current file path
  -> Set Text    -- ^ Explicitly imported module names for the current file
  -> CalleeTarget
  -> Maybe GlobalSymbol
resolveTarget indices curMod curPath curImports = \case
  TargetLocal name ->
    case Map.lookup (curMod, name) (idxByModAndName indices) of
      Just s -> Just s
      Nothing -> case Map.lookup (curPath, name) (idxByPathAndName indices) of
        Just s -> Just s
        Nothing ->
          -- Check wildcard / barrel imports scoped to curImports or curMod
          firstJust
            [ followBarrel indices Set.empty 0 impMod name
            | impMod <- Set.toList (Set.insert curMod curImports)
            ]

  TargetMethod cls m ->
    let qualifiedName = if T.null cls then m else cls <> "." <> m
    in case Map.lookup (curMod, qualifiedName) (idxByModAndName indices) of
      Just s -> Just s
      Nothing ->
        case firstJust [Map.lookup (impM, qualifiedName) (idxByModAndName indices) | impM <- Set.toList curImports] of
          Just s -> Just s
          Nothing ->
            if T.null cls
              then Nothing
              else case Map.lookup qualifiedName (idxByNameOnly indices) of
                     Just [unique] -> Just unique
                     _             -> Nothing

  TargetImported impMod sym ->
    let resolvedMod = normalizeImportSpecifier curPath impMod
        candidatesMods = Set.toList (Set.fromList [resolvedMod, impMod])
    in case firstJust [followBarrel indices Set.empty 0 cm sym | cm <- candidatesMods] of
      Just s -> Just s
      Nothing ->
        if T.null sym && T.isInfixOf "." resolvedMod
          then
            let parts = T.splitOn "." resolvedMod
                subMod = T.intercalate "." (init parts)
                subSym = last parts
            in followBarrel indices Set.empty 0 subMod subSym
          else
            let strippedMod = T.dropWhile (== '.') (T.dropWhile (/= '.') resolvedMod)
            in case followBarrel indices Set.empty 0 strippedMod sym of
              Just s -> Just s
              Nothing ->
                case Map.lookup sym (idxByNameOnly indices) of
                  Just candidates ->
                    case filter (matchesImportSpec resolvedMod impMod . symModule) candidates of
                      (matched : _) -> Just matched
                      []            -> Nothing
                  Nothing -> Nothing

  TargetDynamic _ -> Nothing
  where
    firstJust []             = Nothing
    firstJust (Just x : _)   = Just x
    firstJust (Nothing : xs) = firstJust xs

    matchesImportSpec resM rawM symM =
      symM == resM
        || symM == rawM
        || ("." <> resM) `T.isSuffixOf` ("." <> symM)
        || ("." <> symM) `T.isSuffixOf` ("." <> resM)
        || ("." <> rawM) `T.isSuffixOf` ("." <> symM)
        || ("." <> symM) `T.isSuffixOf` ("." <> rawM)

-- | Resolve caller node to a GlobalSymbol.
resolveCaller :: SymbolIndices -> Text -> FilePath -> CallerNode -> GlobalSymbol
resolveCaller indices curMod curPath = \case
  CallFunction fn ->
    case Map.lookup (curMod, fn) (idxByModAndName indices) of
      Just s  -> s
      Nothing -> GlobalSymbol curPath curMod fn KindFunction (Fingerprint "")
  CallMethod cls m ->
    let qual = cls <> "." <> m
    in case Map.lookup (curMod, qual) (idxByModAndName indices) of
      Just s  -> s
      Nothing -> GlobalSymbol curPath curMod qual KindMethod (Fingerprint "")
  CallTopLevel ->
    GlobalSymbol curPath curMod "<top-level>" KindFunction (Fingerprint "top")

-- | Build the unified repository-level Call Graph across all modules.
buildWholeRepoCallGraph :: [(FilePath, Program)] -> WholeRepoCallGraph
buildWholeRepoCallGraph modules =
  let summaries = map (uncurry extractFileGraphSummary) modules
  in buildWholeRepoCallGraphFromSummaries summaries

convertLocalEdge
  :: SymbolIndices
  -> Text
  -> FilePath
  -> Set Text
  -> CallEdge
  -> Maybe WholeRepoCallEdge
convertLocalEdge indices curMod curPath curImports (CallEdge caller callee cnt isAsync) =
  let callerSym = resolveCaller indices curMod curPath caller
  in case resolveTarget indices curMod curPath curImports callee of
    Nothing -> Nothing
    Just calleeSym ->
      let isCross = symModule callerSym /= symModule calleeSym
                 || symFilePath callerSym /= symFilePath calleeSym
      in Just $ WholeRepoCallEdge
          { wceCaller     = callerSym
          , wceCallee     = calleeSym
          , wceCallCount  = cnt
          , wceIsAsync    = isAsync
          , wceIsCrossMod = isCross
          }

consolidateWholeRepoEdges :: [WholeRepoCallEdge] -> [WholeRepoCallEdge]
consolidateWholeRepoEdges edges =
  let grouped = Map.fromListWith (+)
        [ ((wceCaller e, wceCallee e, wceIsAsync e, wceIsCrossMod e), wceCallCount e)
        | e <- edges
        ]
      rebuilt =
        [ WholeRepoCallEdge c t cnt a isCross
        | ((c, t, a, isCross), cnt) <- Map.toList grouped
        ]
  in sortBy (comparing (\e -> (symModule (wceCaller e), symDeclName (wceCaller e), symModule (wceCallee e), symDeclName (wceCallee e)))) rebuilt

-- | Find all cross-module edges in the WholeRepoCallGraph.
findCrossModuleEdges :: WholeRepoCallGraph -> [WholeRepoCallEdge]
findCrossModuleEdges cg = filter wceIsCrossMod (wcgEdges cg)

-- | Find declared symbols that are never called across the whole repository
-- using zero-allocation transpose in-degree queries on unboxed CSR graph.
findDeadSymbols :: WholeRepoCallGraph -> [GlobalSymbol]
findDeadSymbols cg =
  let (!csr, nodes) = toCSRCallGraph cg
      !trans = transposeCSR csr
      isIgnored s = symDeclName s == "<top-level>"
                 || symDeclName s == "main"
                 || symDeclName s == "__init__"
  in [ s
     | (idx, s) <- zip ([0..] :: [Word32]) nodes
     , csrOutDegree trans idx == 0
     , not (isIgnored s)
     ]

-- | Tarjan's Strongly Connected Components algorithm for whole-repository graphs.
findWholeRepoSCCs :: WholeRepoCallGraph -> [[GlobalSymbol]]
findWholeRepoSCCs = wcgSCCs

-- | Linear Tarjan's Strongly Connected Components algorithm for whole-repository graphs
-- executing directly over unboxed Compressed Sparse Row vectors.
tarjanWholeRepoSCC :: [GlobalSymbol] -> [WholeRepoCallEdge] -> [[GlobalSymbol]]
tarjanWholeRepoSCC [] _ = []
tarjanWholeRepoSCC nodes edges =
  let !nodeVec = V.fromList nodes
      !nodeCount = fromIntegral (V.length nodeVec) :: Word32
      !nodeIndexMap = Map.fromList (zip nodes [0 .. nodeCount - 1])
      resolveIndex s = Map.lookup s nodeIndexMap
      encodeEdgeFlags e =
        let !fSync = if wceIsAsync e then flagCallAsync else flagCallSync
            !fCross = if wceIsCrossMod e then flagCrossModule else flagNone
        in fSync .|. fCross
      rawCsrEdges =
        [ (u, v, encodeEdgeFlags e)
        | e <- edges
        , Just u <- [resolveIndex (wceCaller e)]
        , Just v <- [resolveIndex (wceCallee e)]
        ]
      !csr = buildCSRGraph nodeCount rawCsrEdges
      !sccIndices = tarjanSCC csr
  in [ [ nodeVec V.! fromIntegral idx | idx <- comp ]
     | comp <- sccIndices
     ]

-- | Convert a 'WholeRepoCallGraph' into an unboxed contiguous 'CSRGraph' and its node list.
toCSRCallGraph :: WholeRepoCallGraph -> (CSRGraph, [GlobalSymbol])
toCSRCallGraph cg =
  let !nodes = wcgNodes cg
      !nodeCount = fromIntegral (length nodes) :: Word32
      !nodeIndexMap = Map.fromList (zip nodes [0 .. nodeCount - 1])
      resolveIndex s = Map.lookup s nodeIndexMap
      encodeEdgeFlags e =
        let !fSync = if wceIsAsync e then flagCallAsync else flagCallSync
            !fCross = if wceIsCrossMod e then flagCrossModule else flagNone
        in fSync .|. fCross
      rawEdges =
        [ (u, v, encodeEdgeFlags e)
        | e <- wcgEdges cg
        , Just u <- [resolveIndex (wceCaller e)]
        , Just v <- [resolveIndex (wceCallee e)]
        ]
      !csr = buildCSRGraph nodeCount rawEdges
  in (csr, nodes)

-- | Convert a 'WholeRepoDataFlowGraph' into an unboxed contiguous 'CSRGraph' and its node list.
toCSRDataFlowGraph :: WholeRepoDataFlowGraph -> (CSRGraph, [GlobalSymbol])
toCSRDataFlowGraph dfg =
  let !nodes = wdfNodes dfg
      !nodeCount = fromIntegral (length nodes) :: Word32
      !nodeIndexMap = Map.fromList (zip nodes [0 .. nodeCount - 1])
      resolveIndex s = Map.lookup s nodeIndexMap
      encodeEdgeFlags e =
        if ipdfIsReturnFlow e then flagDataFlowRet else flagDataFlowUse
      rawEdges =
        [ (u, v, encodeEdgeFlags e)
        | e <- wdfEdges dfg
        , Just u <- [resolveIndex (ipdfSourceSymbol e)]
        , Just v <- [resolveIndex (ipdfTargetSymbol e)]
        ]
      !csr = buildCSRGraph nodeCount rawEdges
  in (csr, nodes)

-- | High-performance dual synthesizer: computes both 'WholeRepoCallGraph' and unboxed 'CSRGraph'.
buildCSRCallGraph :: [(FilePath, Program)] -> (WholeRepoCallGraph, CSRGraph)
buildCSRCallGraph modules =
  let !wcg = buildWholeRepoCallGraph modules
      (!csr, _) = toCSRCallGraph wcg
  in (wcg, csr)

-- | High-performance dual synthesizer: computes both 'WholeRepoDataFlowGraph' and unboxed 'CSRGraph'.
buildCSRDataFlow :: [(FilePath, Program)] -> (WholeRepoDataFlowGraph, CSRGraph)
buildCSRDataFlow modules =
  let !wdf = buildWholeRepoDataFlow modules
      (!csr, _) = toCSRDataFlowGraph wdf
  in (wdf, csr)

-- | Extract inter-procedural data-flow graphs across module boundaries.
buildWholeRepoDataFlow :: [(FilePath, Program)] -> WholeRepoDataFlowGraph
buildWholeRepoDataFlow modules =
  let summaries = map (uncurry extractFileGraphSummary) modules
  in buildWholeRepoDataFlowFromSummaries summaries

collectExprCalls :: Expr -> [(Expr, [Expr], [(Text, Expr)])]
collectExprCalls = \case
  ExprCall target args kwargs ->
    (target, args, kwargs) : collectExprCalls target ++ concatMap collectExprCalls args ++ concatMap (collectExprCalls . snd) kwargs
  ExprBinary _ e1 e2 -> collectExprCalls e1 ++ collectExprCalls e2
  ExprUnary _ e -> collectExprCalls e
  ExprAttr e _ -> collectExprCalls e
  ExprSubscript e idx -> collectExprCalls e ++ collectExprCalls idx
  ExprSlice m1 m2 m3 -> maybe [] collectExprCalls m1 ++ maybe [] collectExprCalls m2 ++ maybe [] collectExprCalls m3
  ExprList es -> concatMap collectExprCalls es
  ExprTuple es -> concatMap collectExprCalls es
  ExprDict pairs -> concatMap (\(k, v) -> collectExprCalls k ++ collectExprCalls v) pairs
  ExprSet es -> concatMap collectExprCalls es
  ExprTernary c t f -> collectExprCalls c ++ collectExprCalls t ++ collectExprCalls f
  ExprLambda _ e -> collectExprCalls e
  ExprListComp e comps -> collectExprCalls e ++ concatMap compCalls comps
  ExprDictComp k v comps -> collectExprCalls k ++ collectExprCalls v ++ concatMap compCalls comps
  ExprSetComp e comps -> collectExprCalls e ++ concatMap compCalls comps
  ExprGenerator e comps -> collectExprCalls e ++ concatMap compCalls comps
  ExprWalrus _ e -> collectExprCalls e
  ExprAwait e -> collectExprCalls e
  ExprYield me -> maybe [] collectExprCalls me
  ExprYieldFrom e -> collectExprCalls e
  ExprFormattedString parts -> concatMap fstringCalls parts
  ExprStarred e -> collectExprCalls e
  ExprKwStarred e -> collectExprCalls e
  ExprOptChain e _ -> collectExprCalls e
  ExprNullish e1 e2 -> collectExprCalls e1 ++ collectExprCalls e2
  ExprChanRecv e -> collectExprCalls e
  ExprTryOp e -> collectExprCalls e
  ExprMacroCall _ args -> concatMap collectExprCalls args
  ExprJSX _ attrs children -> concatMap (collectExprCalls . snd) attrs ++ concatMap collectExprCalls children
  _ -> []
  where
    compCalls (CompFor t i ifs) = collectExprCalls t ++ collectExprCalls i ++ concatMap collectExprCalls ifs
    fstringCalls (FStringExpr e _ _) = collectExprCalls e
    fstringCalls _                   = []

collectStmtCalls :: Stmt -> [(Expr, [Expr], [(Text, Expr)])]
collectStmtCalls = \case
  StmtAssign targets val -> concatMap collectExprCalls targets ++ collectExprCalls val
  StmtAnnAssign target ty v -> collectExprCalls target ++ collectExprCalls ty ++ maybe [] collectExprCalls v
  StmtAugAssign t _ v -> collectExprCalls t ++ collectExprCalls v
  StmtExpr e -> collectExprCalls e
  StmtReturn me -> maybe [] collectExprCalls me
  StmtIf c b e -> collectExprCalls c ++ concatMap collectStmtCalls b ++ concatMap collectStmtCalls e
  StmtWhile c b e -> collectExprCalls c ++ concatMap collectStmtCalls b ++ concatMap collectStmtCalls e
  StmtFor t i b e -> collectExprCalls t ++ collectExprCalls i ++ concatMap collectStmtCalls b ++ concatMap collectStmtCalls e
  StmtAsyncFor t i b e -> collectExprCalls t ++ collectExprCalls i ++ concatMap collectStmtCalls b ++ concatMap collectStmtCalls e
  StmtTry b h e f ->
    concatMap collectStmtCalls b
      ++ concatMap (\(me, _, hb) -> maybe [] collectExprCalls me ++ concatMap collectStmtCalls hb) h
      ++ concatMap collectStmtCalls e
      ++ concatMap collectStmtCalls f
  StmtWith items b -> concatMap (\(e, ma) -> collectExprCalls e ++ maybe [] collectExprCalls ma) items ++ concatMap collectStmtCalls b
  StmtAsyncWith items b -> concatMap (\(e, ma) -> collectExprCalls e ++ maybe [] collectExprCalls ma) items ++ concatMap collectStmtCalls b
  StmtAssert e me -> collectExprCalls e ++ maybe [] collectExprCalls me
  StmtRaise me mc -> maybe [] collectExprCalls me ++ maybe [] collectExprCalls mc
  StmtDelete es -> concatMap collectExprCalls es
  StmtMatch s cs ->
    collectExprCalls s
      ++ concatMap (\mc -> collectExprCalls (mcPattern mc) ++ maybe [] collectExprCalls (mcGuard mc) ++ concatMap collectStmtCalls (mcBody mc)) cs
  StmtGo e -> collectExprCalls e
  StmtDefer e -> collectExprCalls e
  StmtChanSend ch val -> collectExprCalls ch ++ collectExprCalls val
  StmtSelect cases -> concatMap (\(sc, b) -> selectCalls sc ++ concatMap collectStmtCalls b) cases
  StmtLoop b -> concatMap collectStmtCalls b
  StmtSwitch expr cases defStmts ->
    collectExprCalls expr
      ++ concatMap (\(c, cStmts) -> collectExprCalls c ++ concatMap collectStmtCalls cStmts) cases
      ++ concatMap collectStmtCalls defStmts
  _ -> []
  where
    selectCalls = \case
      SelectSend ch val -> collectExprCalls ch ++ collectExprCalls val
      SelectRecv _ ch   -> collectExprCalls ch
      SelectDefault     -> []

collectExprVars :: Expr -> Set Text
collectExprVars = \case
  ExprId v -> Set.singleton v
  ExprBinary _ e1 e2 -> Set.union (collectExprVars e1) (collectExprVars e2)
  ExprUnary _ e -> collectExprVars e
  ExprCall t args kw -> Set.unions (collectExprVars t : map collectExprVars args ++ map (collectExprVars . snd) kw)
  ExprAttr e _ -> collectExprVars e
  ExprSubscript e idx -> Set.union (collectExprVars e) (collectExprVars idx)
  ExprSlice m1 m2 m3 -> Set.unions [maybe Set.empty collectExprVars m1, maybe Set.empty collectExprVars m2, maybe Set.empty collectExprVars m3]
  ExprList es -> foldMap collectExprVars es
  ExprTuple es -> foldMap collectExprVars es
  ExprDict pairs -> foldMap (\(k, v) -> Set.union (collectExprVars k) (collectExprVars v)) pairs
  ExprSet es -> foldMap collectExprVars es
  ExprTernary c t f -> Set.unions [collectExprVars c, collectExprVars t, collectExprVars f]
  ExprLambda _ e -> collectExprVars e
  ExprListComp e comps -> Set.union (collectExprVars e) (foldMap compVars comps)
  ExprDictComp k v comps -> Set.unions [collectExprVars k, collectExprVars v, foldMap compVars comps]
  ExprSetComp e comps -> Set.union (collectExprVars e) (foldMap compVars comps)
  ExprGenerator e comps -> Set.union (collectExprVars e) (foldMap compVars comps)
  ExprWalrus v e -> Set.insert v (collectExprVars e)
  ExprAwait e -> collectExprVars e
  ExprYield me -> maybe Set.empty collectExprVars me
  ExprYieldFrom e -> collectExprVars e
  ExprFormattedString parts -> foldMap fstringVars parts
  ExprStarred e -> collectExprVars e
  ExprKwStarred e -> collectExprVars e
  ExprOptChain e _ -> collectExprVars e
  ExprNullish e1 e2 -> Set.union (collectExprVars e1) (collectExprVars e2)
  ExprChanRecv e -> collectExprVars e
  ExprTryOp e -> collectExprVars e
  ExprMacroCall _ args -> foldMap collectExprVars args
  ExprJSX _ attrs children -> Set.unions (map (collectExprVars . snd) attrs ++ map collectExprVars children)
  _ -> Set.empty
  where
    compVars (CompFor t i ifs) = Set.unions (collectExprVars t : collectExprVars i : map collectExprVars ifs)
    fstringVars (FStringExpr e _ _) = collectExprVars e
    fstringVars _                   = Set.empty

buildImportMap :: [ImportDecl] -> Map Text (Text, Text)
buildImportMap imps = Map.fromList (concatMap toEntry imps)
  where
    toEntry (ImportModule m alias) =
      let bound = maybe (lastPart m) id alias
      in [(bound, (m, ""))]
    toEntry (ImportFrom m target) = case target of
      ImportAll -> []
      ImportSymbols syms ->
        [ (maybe sym id alias, (m, sym))
        | (sym, alias) <- syms
        ]

    lastPart m = case T.splitOn "." m of
      [] -> m
      xs -> last xs

resolveExprCalleeTarget :: Map Text (Text, Text) -> Expr -> CalleeTarget
resolveExprCalleeTarget impMap = \case
  ExprId name ->
    case Map.lookup name impMap of
      Just (modName, symName) ->
        let actualSym = if T.null symName then name else symName
        in TargetImported modName actualSym
      Nothing -> TargetLocal name

  ExprAttr (ExprId obj) method ->
    case Map.lookup obj impMap of
      Just (modName, _) -> TargetImported modName method
      Nothing           -> TargetMethod obj method

  ExprAttr (ExprAttr (ExprId pkg) modName) method ->
    let full = pkg <> "." <> modName
    in case Map.lookup full impMap of
      Just (actualMod, _) -> TargetImported actualMod method
      Nothing             -> TargetImported full method

  ExprAttr target method ->
    TargetMethod (T.pack (show target)) method

  ExprAwait inner ->
    resolveExprCalleeTarget impMap inner

  other -> TargetDynamic other

-- | Format WholeRepoCallGraph for human-readable diagnostic display.
formatWholeRepoCallGraph :: WholeRepoCallGraph -> Text
formatWholeRepoCallGraph cg =
  let crossEdges = findCrossModuleEdges cg
      sccs = filter (\c -> length c > 1) (wcgSCCs cg)
  in T.unlines $
    [ "Whole-Repository Call Graph (" <> T.pack (show (length (wcgNodes cg))) <> " symbols, " <> T.pack (show (length (wcgEdges cg))) <> " edges, " <> T.pack (show (length crossEdges)) <> " cross-module)"
    , "--------------------------------------------------------------------------------"
    ] ++
    map formatEdge (wcgEdges cg) ++
    (if null sccs
       then ["\nRecursive Cycles: None"]
       else ["\nRecursive Cycles (SCCs):"] ++ map formatSCC sccs)
  where
    formatEdge e =
      let crossTag = if wceIsCrossMod e then " [cross-module]" else ""
          asyncTag = if wceIsAsync e then " [async]" else ""
          countStr = if wceCallCount e > 1 then " (" <> T.pack (show (wceCallCount e)) <> "x)" else ""
      in "  " <> symModule (wceCaller e) <> ":" <> symDeclName (wceCaller e)
         <> " --> " <> symModule (wceCallee e) <> ":" <> symDeclName (wceCallee e)
         <> crossTag <> asyncTag <> countStr

    formatSCC comp =
      "  Cycle: " <> T.intercalate " <-> " [symModule s <> ":" <> symDeclName s | s <- comp]

-- | Format WholeRepoDataFlowGraph for human-readable diagnostic display.
formatWholeRepoDataFlow :: WholeRepoDataFlowGraph -> Text
formatWholeRepoDataFlow dfg =
  T.unlines $
    [ "Whole-Repository Data Flow Graph (" <> T.pack (show (length (wdfNodes dfg))) <> " symbols, " <> T.pack (show (length (wdfEdges dfg))) <> " flow edges)"
    , "--------------------------------------------------------------------------------"
    ] ++
    map formatFlowEdge (wdfEdges dfg)
  where
    formatFlowEdge e =
      let kind = if ipdfIsReturnFlow e
                   then " [return-flow]"
                   else " [param:" <> T.pack (show (ipdfParamIndex e)) <> "]"
      in "  " <> symModule (ipdfSourceSymbol e) <> ":" <> symDeclName (ipdfSourceSymbol e)
         <> " --(" <> ipdfVarName e <> ")--> "
         <> symModule (ipdfTargetSymbol e) <> ":" <> symDeclName (ipdfTargetSymbol e)
         <> kind

-- ============================================================================
-- Step 3.3: Localized Incremental Graph Delta Propagation
-- ============================================================================

-- | Incrementally updates 'WholeRepoCallGraph' when a subset of files have been modified.
-- Uses reachability-cone edge splicing: ΔG = (G_cached \ E_out(M_old)) ∪ E_out(M_new).
incrementalUpdateWholeRepoCallGraph
  :: WholeRepoCallGraph
  -> [(FilePath, Program)]  -- ^ All repository programs
  -> [FilePath]             -- ^ Subset of modified files
  -> WholeRepoCallGraph
incrementalUpdateWholeRepoCallGraph oldWCG allModules modifiedFiles
  | null modifiedFiles = oldWCG
  | otherwise =
      let summaries = map (uncurry extractFileGraphSummary) allModules
          allSymbols = concatMap fgsSymbols summaries
          barrelMap = buildBarrelMapFromSummaries summaries
          indices = buildSymbolIndices allSymbols barrelMap
          modSet = Set.fromList modifiedFiles
          retainedEdges =
            [ e
            | e <- wcgEdges oldWCG
            , not (Set.member (symFilePath (wceCaller e)) modSet)
            ]
          modSummaries = filter (\s -> Set.member (fgsFilePath s) modSet) summaries
          newEdges =
            [ edge
            | s <- modSummaries
            , let curImps = Set.fromList (fgsImportedMods s)
            , ce <- fgsCallEdges s
            , Just edge <- [convertLocalEdge indices (fgsModuleName s) (fgsFilePath s) curImps ce]
            ]
          combinedEdges = consolidateWholeRepoEdges (retainedEdges ++ newEdges)
          nodes = Set.toAscList (Set.fromList (allSymbols ++ map wceCaller combinedEdges ++ map wceCallee combinedEdges))
          sccs = tarjanWholeRepoSCC nodes combinedEdges
      in WholeRepoCallGraph nodes combinedEdges sccs

-- | Incrementally updates 'WholeRepoDataFlowGraph' when a subset of files have been modified.
incrementalUpdateWholeRepoDataFlow
  :: WholeRepoDataFlowGraph
  -> [(FilePath, Program)]  -- ^ All repository programs
  -> [FilePath]             -- ^ Subset of modified files
  -> WholeRepoDataFlowGraph
incrementalUpdateWholeRepoDataFlow oldWDF allModules modifiedFiles
  | null modifiedFiles = oldWDF
  | otherwise =
      let summaries = map (uncurry extractFileGraphSummary) allModules
          allSymbols = concatMap fgsSymbols summaries
          barrelMap = buildBarrelMapFromSummaries summaries
          indices = buildSymbolIndices allSymbols barrelMap
          modSet = Set.fromList modifiedFiles
          retainedEdges =
            [ e
            | e <- wdfEdges oldWDF
            , not (Set.member (symFilePath (ipdfSourceSymbol e)) modSet)
            ]
          modSummaries = filter (\s -> Set.member (fgsFilePath s) modSet) summaries
          newEdges = concatMap (summaryToDataFlowEdges indices) modSummaries
          uniqueEdges = Set.toAscList (Set.fromList (retainedEdges ++ newEdges))
          nodes = Set.toAscList (Set.fromList (allSymbols ++ map ipdfSourceSymbol uniqueEdges ++ map ipdfTargetSymbol uniqueEdges))
      in WholeRepoDataFlowGraph nodes uniqueEdges

-- | Incrementally updates WholeRepoCallGraph, WholeRepoDataFlowGraph, and their CSR graphs
-- in sub-10ms time using localized reachability-cone delta propagation.
incrementalUpdateWholeRepoGraphs
  :: WholeRepoCallGraph
  -> WholeRepoDataFlowGraph
  -> [(FilePath, Program)]
  -> [FilePath]
  -> (WholeRepoCallGraph, WholeRepoDataFlowGraph, CSRGraph, CSRGraph, Fingerprint, Fingerprint)
incrementalUpdateWholeRepoGraphs oldWCG oldWDF allModules modifiedFiles
  | null modifiedFiles =
      let (!cgCSR, _) = toCSRCallGraph oldWCG
          (!dfCSR, _) = toCSRDataFlowGraph oldWDF
          !fwcg = hashBytes (canonicalizeWholeRepoCallGraph oldWCG)
          !fwdf = hashBytes (canonicalizeWholeRepoDataFlow oldWDF)
      in (oldWCG, oldWDF, cgCSR, dfCSR, fwcg, fwdf)
  | otherwise =
      let !newWCG = incrementalUpdateWholeRepoCallGraph oldWCG allModules modifiedFiles
          !newWDF = incrementalUpdateWholeRepoDataFlow oldWDF allModules modifiedFiles
          (!cgCSR, _) = toCSRCallGraph newWCG
          (!dfCSR, _) = toCSRDataFlowGraph newWDF
          !fwcg = hashBytes (canonicalizeWholeRepoCallGraph newWCG)
          !fwdf = hashBytes (canonicalizeWholeRepoDataFlow newWDF)
      in (newWCG, newWDF, cgCSR, dfCSR, fwcg, fwdf)

-- ============================================================================
-- Per-File Graph Summaries for CNTR\x07 Incremental Whole-Repo Synthesis
-- ============================================================================

-- | Compact serializable inter-procedural call-site data-flow summary.
data CallFlowSite = CallFlowSite
  { cfsCaller  :: !CallerNode
  , cfsCallee  :: !CalleeTarget
  , cfsPosArgs :: ![[Text]]         -- ^ Empty list indicates "<const>"
  , cfsKwArgs  :: ![(Text, [Text])] -- ^ Empty list indicates k <> "=<const>"
  } deriving stock (Eq, Ord, Show, Generic)
    deriving anyclass (NFData)

-- | Compact per-file symbol, import, barrel re-export, call-edge, and data-flow summary persisted in CNTR\x07.
-- Enables whole-repository graph synthesis on warm/dirty runs without re-parsing
-- untouched files from disk.
data FileGraphSummary = FileGraphSummary
  { fgsFilePath     :: !FilePath
  , fgsModuleName   :: !Text
  , fgsImportedMods :: ![Text]
  , fgsReExports    :: ![(Text, Text, Text)]
  , fgsSymbols      :: ![GlobalSymbol]
  , fgsCallEdges    :: ![CallEdge]
  , fgsFlowSites    :: ![CallFlowSite]
  } deriving stock (Eq, Ord, Show, Generic)
    deriving anyclass (NFData)

isDynamicTarget :: CalleeTarget -> Bool
isDynamicTarget (TargetDynamic _) = True
isDynamicTarget _                 = False

-- | Extract a compact 'FileGraphSummary' from a single parsed 'Program'.
extractFileGraphSummary :: FilePath -> Program -> FileGraphSummary
extractFileGraphSummary fp prog =
  let !pathMod = filePathToModuleName fp
      !curMod = case progModules prog of
        (m : _) -> effectiveModuleName fp pathMod (modName m)
        []      -> pathMod
      !(impMods, reExps) = extractImportsAndBarrels fp prog
      !syms = extractProgramSymbols fp prog
      !localCG = buildCallGraph prog
      !callEdges = filter (not . isDynamicTarget . edgeCallee) (cgEdges localCG)
      !allImports = concatMap modImports (progModules prog)
      !impMap = buildImportMap allImports
      !flowSites = concatMap (extractModFlowSites impMap) (progModules prog)
  in FileGraphSummary fp curMod impMods reExps syms callEdges flowSites
  where
    extractModFlowSites impMap (Module _ _ decls stmts) =
      let topSites = concatMap (stmtToFlowSites impMap CallTopLevel) stmts
          declSites = concatMap (declToFlowSites impMap) decls
      in topSites ++ declSites

    declToFlowSites impMap = \case
      DeclFunction fn ->
        concatMap (stmtToFlowSites impMap (CallFunction (fnName fn))) (fnBody fn)
      DeclClass cls ->
        concatMap (\m -> concatMap (stmtToFlowSites impMap (CallMethod (clsName cls) (fnName m))) (fnBody m)) (clsMethods cls)
      DeclStruct st ->
        concatMap (\m -> concatMap (stmtToFlowSites impMap (CallMethod (stName st) (fnName m))) (fnBody m)) (stMethods st)
      DeclTrait tr ->
        concatMap (\m -> concatMap (stmtToFlowSites impMap (CallMethod (trName tr) (fnName m))) (fnBody m)) (trMethods tr)
      DeclImpl imp ->
        concatMap (\m -> concatMap (stmtToFlowSites impMap (CallMethod (impTarget imp) (fnName m))) (fnBody m)) (impMethods imp)
      DeclReceiver rc fn ->
        concatMap (stmtToFlowSites impMap (CallMethod (rcTypeName rc) (fnName fn))) (fnBody fn)
      _ -> []

    stmtToFlowSites impMap caller stmt =
      [ CallFlowSite caller target posArgs kwArgs
      | (calleeExpr, args, kwargs) <- collectStmtCalls stmt
      , let target = resolveExprCalleeTarget impMap calleeExpr
      , not (isDynamicTarget target)
      , let posArgs = [Set.toAscList (collectExprVars arg) | arg <- args]
      , let kwArgs = [(k, Set.toAscList (collectExprVars val)) | (k, val) <- kwargs]
      ]

-- | Synthesize 'WholeRepoCallGraph' directly from cached 'FileGraphSummary' records
-- with zero AST re-parsing. Produces bit-identical output to 'buildWholeRepoCallGraph'.
buildWholeRepoCallGraphFromSummaries :: [FileGraphSummary] -> WholeRepoCallGraph
buildWholeRepoCallGraphFromSummaries summaries =
  let allSymbols = concatMap fgsSymbols summaries
      barrelMap = buildBarrelMapFromSummaries summaries
      indices = buildSymbolIndices allSymbols barrelMap
      rawEdges =
        [ edge
        | s <- summaries
        , let curImps = Set.fromList (fgsImportedMods s)
        , ce <- fgsCallEdges s
        , Just edge <- [convertLocalEdge indices (fgsModuleName s) (fgsFilePath s) curImps ce]
        ]
      consolidatedEdges = consolidateWholeRepoEdges rawEdges
      allNodes = Set.toAscList (Set.fromList (allSymbols ++ map wceCaller consolidatedEdges ++ map wceCallee consolidatedEdges))
      sccs = tarjanWholeRepoSCC allNodes consolidatedEdges
  in WholeRepoCallGraph allNodes consolidatedEdges sccs

-- | Synthesize 'WholeRepoDataFlowGraph' directly from cached 'FileGraphSummary' records
-- with zero AST re-parsing. Produces bit-identical output to 'buildWholeRepoDataFlow'.
buildWholeRepoDataFlowFromSummaries :: [FileGraphSummary] -> WholeRepoDataFlowGraph
buildWholeRepoDataFlowFromSummaries summaries =
  let allSymbols = concatMap fgsSymbols summaries
      barrelMap = buildBarrelMapFromSummaries summaries
      indices = buildSymbolIndices allSymbols barrelMap
      edges = concatMap (summaryToDataFlowEdges indices) summaries
      uniqueEdges = Set.toAscList (Set.fromList edges)
      nodes = Set.toAscList (Set.fromList (allSymbols ++ map ipdfSourceSymbol uniqueEdges ++ map ipdfTargetSymbol uniqueEdges))
  in WholeRepoDataFlowGraph nodes uniqueEdges

summaryToDataFlowEdges :: SymbolIndices -> FileGraphSummary -> [InterProceduralDataFlowEdge]
summaryToDataFlowEdges indices s =
  let curMod = fgsModuleName s
      curPath = fgsFilePath s
      curImps = Set.fromList (fgsImportedMods s)
  in concatMap (siteToEdges curMod curPath curImps) (fgsFlowSites s)
  where
    siteToEdges curMod curPath curImps CallFlowSite{..} =
      let callerSym = resolveCaller indices curMod curPath cfsCaller
      in case resolveTarget indices curMod curPath curImps cfsCallee of
           Nothing -> []
           Just calleeSym ->
             let argEdges = concat
                   [ if null vars
                       then [InterProceduralDataFlowEdge callerSym calleeSym idx "<const>" False]
                       else [InterProceduralDataFlowEdge callerSym calleeSym idx v False | v <- vars]
                   | (idx, vars) <- zip [0..] cfsPosArgs
                   ]
                 kwEdges = concat
                   [ if null vars
                       then [InterProceduralDataFlowEdge callerSym calleeSym (-2) (k <> "=<const>") False]
                       else [InterProceduralDataFlowEdge callerSym calleeSym (-2) (k <> "=" <> v) False | v <- vars]
                   | (k, vars) <- cfsKwArgs
                   ]
                 retEdge = [InterProceduralDataFlowEdge calleeSym callerSym (-1) "<return>" True]
             in argEdges ++ kwEdges ++ retEdge