Pass 3 of 3 · Deep dive of PHAROSPass 1 · OverviewPass 2 · How it works
Code Observatory / Source
Resolve calls conservatively
Same file, then import, then a name unique in the repository. Anything ambiguous stays unlinked.
PINNED SOURCE Copied from the repository at this commit. Source, not an execution.
PHAROS at 975a38dMIT license
build_edgesbackend/app/codeintel/graph.py, lines 144–244Open source
def build_edges(nodes: list[ChunkNode]) -> tuple[list[Edge], GraphStats]: stats = GraphStats() edges: list[Edge] = [] seen: set[tuple[str, str, str]] = set() def add(src: ChunkNode, dst: ChunkNode, name: str, etype: str, resolution: str) -> bool: if src.id == dst.id: return False key = (src.id, dst.id, etype) if key in seen: return False seen.add(key) edges.append(Edge(src.id, dst.id, name, etype, resolution)) return True by_qualified: dict[str, ChunkNode] = {} by_file: dict[str, list[ChunkNode]] = defaultdict(list) modules: dict[str, ChunkNode] = {} by_name: dict[str, list[ChunkNode]] = defaultdict(list) for n in nodes: by_qualified.setdefault(n.qualified_name, n) by_file[n.path].append(n) if n.kind == "module": modules[n.path] = n elif n.kind != "block": by_name[n.name].append(n) by_basename: dict[str, list[ChunkNode]] = defaultdict(list) for path, mod in modules.items(): by_basename[posixpath.basename(path)].append(mod) py_index = _python_module_index([m for m in modules.values() if m.language == "python"]) # contains for file_nodes in by_file.values(): local = {n.qualified_name: n for n in file_nodes} for n in file_nodes: if n.parent and n.parent in local: if add(local[n.parent], n, n.name, "contains", "same_file"): stats.contains += 1 # imports (module -> module) imported_files: dict[str, set[str]] = defaultdict(set) for path, mod in modules.items(): targets: list[ChunkNode] = [] for spec in mod.imports: if mod.language == "python": hit = _resolve_python_import(spec, py_index) targets.extend([hit] if hit else []) else: targets.extend(_resolve_path_import(path, spec, mod.language, modules, by_basename)) for t in targets: if t.path != path: imported_files[path].add(t.path) if add(mod, t, t.qualified_name, "imports", "imported_file"): stats.imports += 1 # calls for n in nodes: if not n.calls or n.kind == "block": continue file_nodes = by_file[n.path] module = modules.get(n.path) import_map = module.import_map if module else {} for name in n.calls: if name in _IGNORED_CALLS or len(name) < 2: continue target, resolution = None, None same = [c for c in file_nodes if c.name == name and c.kind != "module"] if len(same) == 1: target, resolution = same[0], "same_file" elif len(same) > 1: stats.calls_ambiguous += 1 continue if target is None and name in import_map: qual = import_map[name] hit = by_qualified.get(qual) if hit is None: # Imported module path may carry a src/ prefix in the repo. cands = [c for q, c in by_qualified.items() if q.endswith("." + qual)] hit = cands[0] if len(cands) == 1 else None if hit is not None and hit.kind != "module": target, resolution = hit, "imported_file" # Python has an exact import map (above); this file-level # heuristic is for languages without one (JS, Go, C, ...). if target is None and n.language != "python" and imported_files.get(n.path): cands = [c for c in by_name.get(name, []) if c.path in imported_files[n.path]] if len(cands) == 1: target, resolution = cands[0], "imported_file" if target is None: cands = by_name.get(name, []) if len(cands) == 1: target, resolution = cands[0], "unique_name" elif len(cands) > 1: stats.calls_ambiguous += 1 continue if target is None: stats.calls_unresolved += 1 continue if add(n, target, name, "calls", resolution): stats.calls_resolved += 1 stats.by_resolution[resolution] += 1 return edges, statsWhere this comes from
- Commit
975a38d5d17839c26ee219295a5be6ac66b6d910- File
backend/app/codeintel/graph.py- Lines
- 144–244
- Git blob
3791a0283d18722675047af567570cb2c1911c63- License
- MIT, © 2024–2026 Pharos Contributors. Notice
Read it on GitHub at this commit
Check it yourself
From a clone of the repository, the first command prints the blob above and the second prints these lines.
git rev-parse 975a38d:backend/app/codeintel/graph.py
git show 975a38d:backend/app/codeintel/graph.py | sed -n '144,244p'