descry-codegraph 0.3.0__py3-none-any.whl → 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
descry/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  """Descry — Codebase knowledge graph: polyglot indexer, semantic search, call-graph analysis."""
2
2
 
3
- __version__ = "0.3.0"
3
+ __version__ = "0.3.2"
4
4
  __all__ = ["__version__"]
descry/_graph.py CHANGED
@@ -11,6 +11,11 @@ from pathlib import Path
11
11
 
12
12
  CURRENT_SCHEMA = 2
13
13
 
14
+ # The edges by which one symbol calls or constructs another: its callers
15
+ CALL_RELATIONS = frozenset(
16
+ {"CALLS", "CALLS_RESOLVED", "INSTANTIATES", "INSTANTIATES_RESOLVED"}
17
+ )
18
+
14
19
 
15
20
  class GraphSchemaError(Exception):
16
21
  """Raised when a graph file is not a graph this version can read.
@@ -107,4 +112,18 @@ def load_graph_with_schema(path: Path | str) -> dict:
107
112
  f"run 'descry index' to rebuild."
108
113
  )
109
114
  _check_shape(data)
115
+ count_callers(data["nodes"], data["edges"])
110
116
  return data
117
+
118
+
119
+ def count_callers(nodes: list, edges: list) -> None:
120
+ """Set each node's `in_degree` to the number of its callers: the
121
+ symbols whose calls are resolved to it, not those an unresolved call
122
+ only may be. A graph is counted again as it loads, so one written by an
123
+ older descry counts them the same way."""
124
+ callers: dict[str, set] = {}
125
+ for edge in edges:
126
+ if edge["relation"] in CALL_RELATIONS:
127
+ callers.setdefault(edge["target"], set()).add(edge["source"])
128
+ for node in nodes:
129
+ node["metadata"]["in_degree"] = len(callers.get(node["id"], ()))
descry/cli.py CHANGED
@@ -302,7 +302,12 @@ def main():
302
302
  choices=["forward", "backward"],
303
303
  help="Trace direction (default: forward)",
304
304
  )
305
- sub.add_argument("--depth", type=int, default=3, help="Trace depth (default: 3)")
305
+ sub.add_argument(
306
+ "--depth",
307
+ type=int,
308
+ default=3,
309
+ help="Levels of calls below the start (default: 3)",
310
+ )
306
311
  sub.add_argument("--target", default=None, help="Target symbol to reach")
307
312
  sub.add_argument(
308
313
  "--inline-threshold",
@@ -368,7 +373,7 @@ def main():
368
373
 
369
374
  # impls
370
375
  sub = subparsers.add_parser(
371
- "impls", help="Find all implementations of a trait method"
376
+ "impls", help="Find all implementations of a Rust trait method"
372
377
  )
373
378
  sub.add_argument("method", help="Method name")
374
379
  sub.add_argument("--trait-name", default=None, help="Filter by trait name")
descry/embeddings.py CHANGED
@@ -12,6 +12,7 @@ Usage:
12
12
 
13
13
  import contextlib
14
14
  import hashlib
15
+ import importlib.util
15
16
  import json
16
17
  import logging
17
18
  import os
@@ -347,13 +348,14 @@ def _resolve_cache_dir(graph_path: Path, cache_dir: str | None = None) -> Path:
347
348
  # Try to import embedding dependencies
348
349
  try:
349
350
  import numpy as np
350
- from sentence_transformers import SentenceTransformer
351
-
352
- EMBEDDINGS_AVAILABLE = True
353
351
  except ImportError:
354
- EMBEDDINGS_AVAILABLE = False
355
352
  np = None
356
- SentenceTransformer = None
353
+ # Imported by _load_sentence_transformer: it brings in torch, which takes
354
+ # seconds a command that loads no model should not spend.
355
+ SentenceTransformer = None
356
+ EMBEDDINGS_AVAILABLE = (
357
+ np is not None and importlib.util.find_spec("sentence_transformers") is not None
358
+ )
357
359
 
358
360
 
359
361
  def embeddings_available() -> bool:
@@ -376,6 +378,9 @@ def _load_sentence_transformer(model_name: str, revision: str | None = None):
376
378
  model_name: Registry alias, HuggingFace repo id, or local path.
377
379
  revision: Overrides the registry's pinned revision.
378
380
  """
381
+ global SentenceTransformer
382
+ if SentenceTransformer is None:
383
+ from sentence_transformers import SentenceTransformer
379
384
  spec = resolve_model_spec(model_name)
380
385
  target = spec.repo_id if spec else model_name
381
386
  if revision is None and spec:
descry/generate.py CHANGED
@@ -8388,15 +8388,6 @@ class PythonParser(BaseParser):
8388
8388
  return f"[{elts}]"
8389
8389
  return "Complex"
8390
8390
 
8391
- def _get_args_info(self, args_node):
8392
- args_list = []
8393
- for arg in args_node.args:
8394
- arg_str = arg.arg
8395
- if arg.annotation:
8396
- arg_str += f": {self._get_type_annotation(arg.annotation)}"
8397
- args_list.append(arg_str)
8398
- return args_list
8399
-
8400
8391
  def _get_param_types(self, args_node):
8401
8392
  """Extract parameter types as a list for metadata storage."""
8402
8393
  param_types = []
@@ -8492,12 +8483,14 @@ class PythonParser(BaseParser):
8492
8483
  is_method = parent_id in self.class_ids
8493
8484
  node_type = "Method" if is_method else "Function"
8494
8485
 
8495
- args = self._get_args_info(node.args)
8496
8486
  param_types = self._get_param_types(node.args)
8497
8487
  return_type = (
8498
- self._get_type_annotation(node.returns) if node.returns else "None"
8488
+ self._get_type_annotation(node.returns) if node.returns else ""
8499
8489
  )
8500
- signature = f"def {node.name}({', '.join(args)}) -> {return_type}"
8490
+ keyword = "async def" if isinstance(node, ast.AsyncFunctionDef) else "def"
8491
+ signature = f"{keyword} {node.name}({ast.unparse(node.args)})"
8492
+ if node.returns:
8493
+ signature += f" -> {ast.unparse(node.returns)}"
8501
8494
 
8502
8495
  self.builder.add_node(
8503
8496
  func_id,
@@ -25440,16 +25433,10 @@ class CodeGraphBuilder:
25440
25433
  output_path: Path to write the graph JSON
25441
25434
  scip_index: Optional ScipIndex for type-aware resolution
25442
25435
  """
25443
- from descry._graph import CURRENT_SCHEMA
25436
+ from descry._graph import CURRENT_SCHEMA, count_callers
25444
25437
 
25445
25438
  self.resolve_references(scip_index=scip_index)
25446
- in_degree = {}
25447
- for edge in self.edges:
25448
- t = edge["target"]
25449
- if t:
25450
- in_degree[t] = in_degree.get(t, 0) + 1
25451
- for node in self.nodes:
25452
- node["metadata"]["in_degree"] = in_degree.get(node["id"], 0)
25439
+ count_callers(self.nodes, self.edges)
25453
25440
  # Atomic write: stream to a sibling tmp file then os.replace. A
25454
25441
  # concurrent reader (MCP prewarm, web `/api/health`, another CLI
25455
25442
  # invocation) that stats the destination mid-write would otherwise
descry/git_history.py CHANGED
@@ -758,7 +758,7 @@ class GitHistoryAnalyzer:
758
758
 
759
759
  # Sort by commit count
760
760
  sorted_symbols = sorted(
761
- symbol_commits.items(), key=lambda x: len(x[1]), reverse=True
761
+ symbol_commits.items(), key=lambda x: (-len(x[1]), x[0])
762
762
  )[:limit]
763
763
 
764
764
  lines = []
@@ -923,7 +923,7 @@ class GitHistoryAnalyzer:
923
923
 
924
924
  # SYMBOLS MODE
925
925
  sorted_symbols = sorted(
926
- symbol_commits.items(), key=lambda x: len(x[1]), reverse=True
926
+ symbol_commits.items(), key=lambda x: (-len(x[1]), x[0])
927
927
  )[:limit]
928
928
 
929
929
  symbols = []
@@ -983,7 +983,7 @@ class GitHistoryAnalyzer:
983
983
  frequent_pairs = [
984
984
  (pair, count) for pair, count in pair_counts.items() if count >= 2
985
985
  ]
986
- frequent_pairs.sort(key=lambda x: x[1], reverse=True)
986
+ frequent_pairs.sort(key=lambda x: (-x[1], x[0]))
987
987
 
988
988
  for (sym_a, sym_b), count in frequent_pairs[:limit]:
989
989
  _, name_a, loc_a = self._get_node_display(sym_a)
@@ -1081,7 +1081,7 @@ class GitHistoryAnalyzer:
1081
1081
  frequent_pairs = [
1082
1082
  (pair, count) for pair, count in pair_counts.items() if count >= 2
1083
1083
  ]
1084
- frequent_pairs.sort(key=lambda x: x[1], reverse=True)
1084
+ frequent_pairs.sort(key=lambda x: (-x[1], x[0]))
1085
1085
  frequent_pairs = frequent_pairs[:limit]
1086
1086
 
1087
1087
  if frequent_pairs:
@@ -1660,26 +1660,43 @@ class GitHistoryAnalyzer:
1660
1660
  f"(+{stats['added']}/-{stats['removed']})"
1661
1661
  )
1662
1662
 
1663
- # Show callers if requested
1664
1663
  if show_callers and self.querier:
1665
- sym_name = name.split(".")[-1] # Get base name
1666
- callers = self.querier.get_callers(sym_name)
1664
+ callers, total, possible = self._symbol_callers(node_id)
1667
1665
  if callers:
1668
- caller_names = []
1669
- for caller_id in callers[:5]:
1670
- c_node = self.querier.get_node_info(caller_id)
1671
- if c_node:
1672
- caller_names.append(c_node["metadata"].get("name", "?"))
1673
- else:
1674
- # Extract name from node ID
1675
- parts = caller_id.split("::")
1676
- caller_names.append(parts[-1] if parts else "?")
1677
-
1678
- more = f", +{len(callers) - 5} more" if len(callers) > 5 else ""
1679
- lines.append(f" Callers: {', '.join(caller_names)}{more}")
1666
+ names = ", ".join(
1667
+ c["name"] + self.querier.one_of(c["candidates"])
1668
+ for c in callers
1669
+ )
1670
+ hidden = possible - sum(bool(c["candidates"]) for c in callers)
1671
+ hidden_certain = total - len(callers) - hidden
1672
+ more = (
1673
+ f"; {self.querier.more_callers(hidden_certain, hidden)}"
1674
+ if total > len(callers)
1675
+ else ""
1676
+ )
1677
+ lines.append(f" Callers: {names}{more}")
1680
1678
 
1681
1679
  return "\n".join(lines)
1682
1680
 
1681
+ def _symbol_callers(self, node_id):
1682
+ """The first five callers of the symbol ``node_id``, certain ones
1683
+ first, each ``{"id", "name", "candidates"}`` where ``candidates`` is
1684
+ the number an unresolved call reaching it only may be one of; the
1685
+ number of its callers; and how many of those only may be."""
1686
+ caller_ids, ambiguous = self.querier.ordered_callers(node_id)
1687
+ callers = []
1688
+ for caller_id in caller_ids[:5]:
1689
+ c_node = self.querier.get_node_info(caller_id)
1690
+ name = (
1691
+ c_node["metadata"].get("name", "?")
1692
+ if c_node
1693
+ else caller_id.split("::")[-1]
1694
+ )
1695
+ callers.append(
1696
+ {"id": caller_id, "name": name, "candidates": ambiguous.get(caller_id)}
1697
+ )
1698
+ return callers, len(caller_ids), len(ambiguous)
1699
+
1683
1700
  def get_changes_structured(
1684
1701
  self,
1685
1702
  commit_range: str | None = None,
@@ -1742,22 +1759,11 @@ class GitHistoryAnalyzer:
1742
1759
  }
1743
1760
 
1744
1761
  if show_callers and self.querier:
1745
- sym_name = name.split(".")[-1]
1746
- caller_ids = self.querier.get_callers(sym_name)
1747
- if caller_ids:
1748
- callers = []
1749
- for caller_id in caller_ids[:5]:
1750
- c_node = self.querier.get_node_info(caller_id)
1751
- c_name = (
1752
- c_node["metadata"].get("name", "?")
1753
- if c_node
1754
- else caller_id.split("::")[-1]
1755
- if "::" in caller_id
1756
- else "?"
1757
- )
1758
- callers.append({"id": caller_id, "name": c_name})
1762
+ callers, total, possible = self._symbol_callers(node_id)
1763
+ if callers:
1759
1764
  entry["callers"] = callers
1760
- entry["callers_total"] = len(caller_ids)
1765
+ entry["callers_total"] = total
1766
+ entry["callers_possible"] = possible
1761
1767
 
1762
1768
  symbols.append(entry)
1763
1769