Back to the room

Pass 3 of 3 · Deep dive of PHAROSPass 1 · OverviewPass 2 · How it works

Code Observatory / method

_expand_graph

codeintel.retrieval.Retriever._expand_graph

PINNED SOURCE Source and edges from the index at this commit. An execution appears only where one was recorded.

PHAROS at 975a38dMIT license

Around this symbol

Search all symbols

Loading

Calls (3)
SymbolFileResolved by
Evidenceretrieval.pysame file
Hitretrieval.pysame file
_filter_clausesretrieval.pysame file
Called by (1)
SymbolFileResolved by
searchretrieval.pysame file
Inside (1)
SymbolFileResolved by
Retrieverretrieval.pysame file
codeintel.retrieval.Retriever._expand_graphbackend/app/codeintel/retrieval.py, lines 465–509Open source
    def _expand_graph(self, hits: dict[str, Hit], filters: Filters) -> dict[str, Hit]:        """Add callers / callees of the top hits that retrieval did not find.        Conservative by design: a neighbour enters with ``seed score x        weight`` (so it ranks below the hit that pulled it in) and chunks        already retrieved get no bonus. An earlier additive variant let        containers (a class containing several hits) outrank the best        direct match.        """        seeds = sorted(hits.values(), key=lambda h: (-h.score, h.chunk_id))[:GRAPH_SEEDS]        seed_score = {h.chunk_id: h.score for h in seeds}        seed_ids = [uuid.UUID(h.chunk_id) for h in seeds]        rows = self.db.execute(            select(CodeEdge.source_chunk_id, CodeEdge.target_chunk_id, CodeEdge.edge_type).where(                CodeEdge.edge_type == "calls",                or_(CodeEdge.source_chunk_id.in_(seed_ids), CodeEdge.target_chunk_id.in_(seed_ids)),            )        ).all()        candidates: dict[str, tuple[float, str, str]] = {}        for src, dst, _etype in rows:            src, dst = str(src), str(dst)            if src in seed_score:                seed, neighbor, relation = src, dst, "calls"            else:                seed, neighbor, relation = dst, src, "called_by"            if neighbor in hits:                continue            score = seed_score[seed] * GRAPH_WEIGHTS[relation]            if neighbor not in candidates or score > candidates[neighbor][0]:                candidates[neighbor] = (score, seed, relation)        if candidates and (filters.languages or filters.kinds or filters.path_prefix or filters.repository_ids):            allowed = {                str(c)                for c in self.db.execute(                    select(DocumentChunk.id).where(                        DocumentChunk.id.in_([uuid.UUID(n) for n in candidates]),                        *self._filter_clauses(filters),                    )                ).scalars()            }            candidates = {k: v for k, v in candidates.items() if k in allowed}        for neighbor, (score, seed, relation) in candidates.items():            h = hits[neighbor] = Hit(chunk_id=neighbor, score=score, evidence=Evidence())            h.evidence.graph.append({"via": seed, "relation": relation})        return hits

What the index recorded

Signature
def _expand_graph(self, hits: dict[str, Hit], filters: Filters) -> dict[str, Hit]:
File
backend/app/codeintel/retrieval.py
Lines
465–509
Parser
python-ast
Git blob
884729a39ae0b3d340ad83f75946677dcddf3791
Call sites
16 named, 5 resolved, 11 left unlinked
Outside this subset
3 more edges reach code outside the graph shown here

Read it on GitHub at this commit