Thread-safe graph contexts: one pinned default plus an LRU of projects.
| 140 | |
| 141 | |
| 142 | class _GraphContextCache: |
| 143 | """Thread-safe graph contexts: one pinned default plus an LRU of projects.""" |
| 144 | |
| 145 | def __init__(self, max_contexts: int): |
| 146 | self._max_contexts = max_contexts |
| 147 | self._entries: OrderedDict[str, dict] = OrderedDict() |
| 148 | self._pinned: dict[str, dict] = {} |
| 149 | self._lock = threading.Lock() |
| 150 | # One in-flight build per (resolved_path, pinned); keyed separately |
| 151 | # from `_entries`/`_pinned` so a rebuild in progress for one path |
| 152 | # never blocks a lookup for another (#5177). |
| 153 | self._loading: dict[tuple[str, bool], threading.Event] = {} |
| 154 | |
| 155 | def _load_entry(self, resolved_path: str, key: tuple[int, int]) -> dict: |
| 156 | """Build one entry for an already-resolved path and known file key. |
| 157 | |
| 158 | ``_load_graph`` is also used by the CLI, where invalid input terminates |
| 159 | the process. A client-supplied ``project_path`` must instead become a |
| 160 | tool error, so the shared MCP server can continue serving other graphs. |
| 161 | """ |
| 162 | try: |
| 163 | graph = _load_graph(resolved_path) |
| 164 | except SystemExit as exc: |
| 165 | raise RuntimeError(f"could not load graph.json at {resolved_path}") from exc |
| 166 | # Warm the index before exposing the graph so its first query does not |
| 167 | # pay the expensive build cost. |
| 168 | _get_trigram_index(graph) |
| 169 | communities = _communities_from_graph(graph) |
| 170 | entry = { |
| 171 | "key": key, |
| 172 | "G": graph, |
| 173 | "communities": communities, |
| 174 | } |
| 175 | return entry |
| 176 | |
| 177 | def load(self, resolved_path: str, *, pinned: bool = False) -> tuple[nx.Graph, dict[int, list[str]]]: |
| 178 | """Return a fresh context, retaining project contexts by LRU order. |
| 179 | |
| 180 | ``resolved_path`` is resolved by the caller, making this method the |
| 181 | sole owner of file statting and cache-key construction. |
| 182 | |
| 183 | ``pinned=True`` is reserved for the server's configured default graph; |
| 184 | it remains warm without consuming a project-cache slot. |
| 185 | |
| 186 | The actual rebuild (``_load_entry``, which parses graph.json and builds |
| 187 | its trigram index) runs outside ``_lock`` so a slow reload of one path |
| 188 | never blocks a request for another (#5177). While a rebuild for THIS |
| 189 | path is already in flight, a caller that still has an old entry for it |
| 190 | gets that stale entry back immediately rather than queuing behind the |
| 191 | rebuild; a caller with no entry yet (the path's first ever load) has |
| 192 | nothing stale to serve and waits for the one in-flight build instead |
| 193 | of starting a redundant second one. |
| 194 | """ |
| 195 | entries = self._pinned if pinned else self._entries |
| 196 | loading_key = (resolved_path, pinned) |
| 197 | with self._lock: |
| 198 | try: |
| 199 | stat_result = Path(resolved_path).stat() |
no outgoing calls