java-codebase-rag 0.11.2__py3-none-any.whl → 0.12.1__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.
Files changed (73) hide show
  1. java_codebase_rag-0.12.1.dist-info/METADATA +35 -0
  2. java_codebase_rag-0.12.1.dist-info/RECORD +4 -0
  3. {java_codebase_rag-0.11.2.dist-info → java_codebase_rag-0.12.1.dist-info}/WHEEL +1 -1
  4. java_codebase_rag/_fdlimit.py +0 -56
  5. java_codebase_rag/_stdio.py +0 -32
  6. java_codebase_rag/_version.py +0 -35
  7. java_codebase_rag/absence/__init__.py +0 -0
  8. java_codebase_rag/absence/absence_diagnosis.py +0 -700
  9. java_codebase_rag/absence/absence_types.py +0 -124
  10. java_codebase_rag/absence/absence_vocab.py +0 -460
  11. java_codebase_rag/analysis/__init__.py +0 -0
  12. java_codebase_rag/analysis/pr_analysis.py +0 -563
  13. java_codebase_rag/analysis/resolve_service.py +0 -740
  14. java_codebase_rag/ast/__init__.py +0 -0
  15. java_codebase_rag/ast/ast_java.py +0 -2825
  16. java_codebase_rag/ast/brownfield_events.py +0 -58
  17. java_codebase_rag/ast/chunk_heuristics.py +0 -62
  18. java_codebase_rag/cli.py +0 -1215
  19. java_codebase_rag/cli_format.py +0 -85
  20. java_codebase_rag/cli_progress.py +0 -94
  21. java_codebase_rag/config.py +0 -833
  22. java_codebase_rag/eval/__init__.py +0 -1
  23. java_codebase_rag/eval/ground_truth.py +0 -100
  24. java_codebase_rag/eval/metrics.py +0 -107
  25. java_codebase_rag/eval/runner.py +0 -556
  26. java_codebase_rag/graph/__init__.py +0 -0
  27. java_codebase_rag/graph/build_ast_graph.py +0 -4471
  28. java_codebase_rag/graph/graph_enrich.py +0 -1937
  29. java_codebase_rag/graph/graph_types.py +0 -224
  30. java_codebase_rag/graph/java_ontology.py +0 -465
  31. java_codebase_rag/graph/ladybug_queries.py +0 -2213
  32. java_codebase_rag/graph/path_filtering.py +0 -477
  33. java_codebase_rag/index/__init__.py +0 -0
  34. java_codebase_rag/index/java_index_flow_lancedb.py +0 -734
  35. java_codebase_rag/index/java_index_v1_common.py +0 -33
  36. java_codebase_rag/install_data/__init__.py +0 -0
  37. java_codebase_rag/install_data/agents/explorer-rag-cli.md +0 -108
  38. java_codebase_rag/install_data/agents/explorer-rag-enhanced.md +0 -152
  39. java_codebase_rag/install_data/skills/explore-codebase/SKILL.md +0 -165
  40. java_codebase_rag/install_data/skills/explore-codebase-cli/SKILL.md +0 -107
  41. java_codebase_rag/installer.py +0 -2188
  42. java_codebase_rag/jrag.py +0 -4531
  43. java_codebase_rag/jrag_envelope.py +0 -1107
  44. java_codebase_rag/jrag_hints.py +0 -204
  45. java_codebase_rag/jrag_render.py +0 -926
  46. java_codebase_rag/lance_optimize.py +0 -264
  47. java_codebase_rag/mcp/__init__.py +0 -0
  48. java_codebase_rag/mcp/mcp_hints.py +0 -932
  49. java_codebase_rag/mcp/mcp_v2.py +0 -1916
  50. java_codebase_rag/mcp/server.py +0 -884
  51. java_codebase_rag/pipeline.py +0 -531
  52. java_codebase_rag/progress.py +0 -570
  53. java_codebase_rag/read_payloads.py +0 -781
  54. java_codebase_rag/search/__init__.py +0 -0
  55. java_codebase_rag/search/index_common.py +0 -10
  56. java_codebase_rag/search/search_lancedb.py +0 -1296
  57. java_codebase_rag/search/search_lexical.py +0 -449
  58. java_codebase_rag/search/search_scoring.py +0 -523
  59. java_codebase_rag/watch/__init__.py +0 -0
  60. java_codebase_rag/watch/client.py +0 -230
  61. java_codebase_rag/watch/daemon.py +0 -396
  62. java_codebase_rag/watch/lock.py +0 -201
  63. java_codebase_rag/watch/paths.py +0 -76
  64. java_codebase_rag/watch/protocol.py +0 -122
  65. java_codebase_rag/watch/server.py +0 -273
  66. java_codebase_rag/watch/warm.py +0 -105
  67. java_codebase_rag/watch/watcher.py +0 -370
  68. java_codebase_rag-0.11.2.dist-info/METADATA +0 -331
  69. java_codebase_rag-0.11.2.dist-info/RECORD +0 -71
  70. java_codebase_rag-0.11.2.dist-info/entry_points.txt +0 -4
  71. java_codebase_rag-0.11.2.dist-info/licenses/LICENSE +0 -21
  72. java_codebase_rag-0.11.2.dist-info/top_level.txt +0 -1
  73. /java_codebase_rag/__init__.py → /java_codebase_rag-0.12.1.dist-info/top_level.txt +0 -0
@@ -1,523 +0,0 @@
1
- #!/usr/bin/env python3
2
- """Dependency-free scoring & dedup primitives shared by the search backends.
3
-
4
- Imported by both the vector backend (`search_lancedb`) and the lexical backend
5
- (`search_lexical`). This module MUST NOT import lancedb / torch /
6
- sentence_transformers / cocoindex — it is imported on graph-only (macOS Intel)
7
- installs where those packages are absent (see pyproject.toml PEP 508 markers).
8
-
9
- Everything here is pure-Python dict/list math with no third-party deps.
10
- """
11
-
12
- from __future__ import annotations
13
-
14
- import json
15
- import re
16
- from dataclasses import dataclass
17
-
18
- # Name of the LadybugDB FTS (Okapi BM25) index over Symbol.search_text (fork A).
19
- # Shared by the build path (build_ast_graph._ensure_symbol_fts_index) and the
20
- # query path (search_lexical.run_lexical_search) so the two never drift.
21
- SYMBOL_FTS_INDEX = "sym_fts"
22
-
23
- # Over-fetch multiplier for dedup: fetch 4x to absorb per-FQN chunk multiplicity
24
- # so that after collapsing by primary_type_fqn, a page stays full and the +1
25
- # truncation sentinel survives. The formula: need = max((limit + offset) * 4, limit + offset + 1)
26
- DEDUP_OVERFETCH = 4
27
-
28
- _IMPORT_DISTANCE_PENALTY = 0.08
29
- _IMPORT_HYBRID_SCORE_FACTOR = 0.88
30
-
31
- # Bonus for chunks whose declared symbols (method / field names) share tokens with
32
- # the query. Behavioural queries like "what happens when a client message arrives"
33
- # should float chunks containing `processClientMessage` above ones that only
34
- # enqueue; this is a cheap, query-dependent signal computed at rank time.
35
- _SYMBOL_MATCH_BONUS_PER_HIT = 0.03
36
- _SYMBOL_MATCH_BONUS_CAP = 0.06
37
-
38
- # Action verbs that typically mark behavioural entry points in this codebase.
39
- # A chunk whose symbols begin with one of these verbs earns a small flat bump
40
- # — again only for java chunks and only when role-filtering is off.
41
- _ACTION_VERB_PREFIXES: tuple[str, ...] = (
42
- "process", "handle", "on", "pick", "select", "assign",
43
- "notify", "dispatch", "publish", "consume", "route",
44
- "trigger", "enqueue", "distribute", "update", "create",
45
- "apply", "resolve", "reassign", "close", "open",
46
- )
47
- _ACTION_VERB_BONUS = 0.02
48
-
49
- # Type-name overlap bonus. The class name is a much stronger discovery signal
50
- # than any individual method, because class naming in this codebase encodes
51
- # the domain concept (`DistributionChunkService`, `OperatorSessionService`,
52
- # `JoinOperatorController`). So we reward overlap between query tokens and the
53
- # simple name of `primary_type_fqn` more heavily than per-method overlap, and
54
- # we stack it on top of the existing `_symbol_bonus`.
55
- _TYPE_MATCH_BONUS_PER_HIT = 0.05
56
- _TYPE_MATCH_BONUS_CAP = 0.10
57
-
58
- _STOPWORDS: frozenset[str] = frozenset({
59
- "a", "an", "the", "is", "are", "was", "were", "be", "been", "being",
60
- "to", "of", "in", "on", "at", "by", "for", "with", "from", "as", "or",
61
- "and", "but", "if", "then", "else", "when", "what", "how", "why", "does",
62
- "do", "did", "has", "have", "had", "this", "that", "these", "those", "it",
63
- "its", "new", "no", "not", "will", "would", "should", "can", "could",
64
- "may", "might", "happens", "happen", "happened", "get", "gets", "got",
65
- })
66
-
67
- # Role-aware reweighting for Java chunks. Positive values favour actionable
68
- # behavioural code (entrypoints, orchestrators, integrations) over configuration,
69
- # schema, and persistence stubs for "what happens when..."-style queries.
70
- # Applied to the similarity score (higher = better); distance-based sort subtracts
71
- # the weight. Skipped when caller filters explicitly by role.
72
- _ROLE_SCORE_WEIGHTS: dict[str, float] = {
73
- "CONTROLLER": 0.10,
74
- "SERVICE": 0.08,
75
- "CLIENT": 0.06,
76
- "COMPONENT": 0.03,
77
- "REPOSITORY": 0.02,
78
- "MAPPER": 0.00,
79
- "OTHER": 0.00,
80
- "ENTITY": -0.06,
81
- "CONFIG": -0.10,
82
- # DTOs are passive data carriers; they almost never answer "how/what
83
- # happens" queries. Penalty is slightly stronger than ENTITY so a DTO
84
- # with a great embedding match still loses to a mediocre SERVICE hit.
85
- "DTO": -0.08,
86
- }
87
-
88
-
89
- def _rrf_max(num_lists: int, k: int = 60) -> float:
90
- """Return the theoretical maximum RRF score for N-list fusion.
91
-
92
- Reciprocal Rank Fusion (RRF) bounds each contribution to ≤ 1/(rank + k).
93
- For N fused lists, the maximum possible sum is N/(k + 1) (achieved when
94
- an item ranks #1 across all lists).
95
-
96
- Args:
97
- num_lists: Number of ranked lists being fused (e.g., 2 for vector+lexical).
98
- k: The RRF constant (default 60 per the original paper).
99
-
100
- Returns:
101
- The maximum RRF contribution: num_lists / (k + 1).
102
- """
103
- return num_lists / (k + 1)
104
-
105
-
106
- # Allowed list names in a RankConfig: vector is always required (it is the
107
- # backbone retrieval signal); graph and bm25 are optional fusion participants.
108
- # The "bm25" list is wired in ``search_lancedb._graph_expand_merge`` (LadybugDB
109
- # FTS candidate fetch), which fuses BM25-ranked Symbol candidates as a third
110
- # RRF list alongside vector and graph.
111
- _RANK_LIST_NAMES: frozenset[str] = frozenset({"vector", "graph", "bm25"})
112
-
113
-
114
- @dataclass(frozen=True)
115
- class RankConfig:
116
- """Which ranked lists to fuse and the RRF constant to fuse them with.
117
-
118
- This is a dep-free value object (no lancedb/torch) so it can be constructed
119
- on every install flavor, including graph-only (macOS Intel). It is plumbed
120
- through ``run_search`` → ``_graph_expand_merge`` to control (a) which lists
121
- contribute to the final RRF fusion and (b) the ``k`` constant passed into
122
- ``_rrf_merge``.
123
-
124
- Attributes:
125
- lists: Subset of ``{"vector", "graph", "bm25"}``. Must contain
126
- ``"vector"`` (the backbone retrieval signal) and be non-empty.
127
- rrf_k: The RRF constant (default 60 per the original paper). Must be ≥ 1.
128
- """
129
-
130
- lists: frozenset[str]
131
- rrf_k: int = 60
132
-
133
- def __post_init__(self) -> None:
134
- if not isinstance(self.lists, frozenset) or not self.lists:
135
- raise ValueError("RankConfig.lists must be a non-empty frozenset")
136
- if "vector" not in self.lists:
137
- raise ValueError(
138
- "RankConfig.lists must contain 'vector' (the backbone signal)"
139
- )
140
- unknown = self.lists - _RANK_LIST_NAMES
141
- if unknown:
142
- raise ValueError(
143
- f"RankConfig.lists has unknown names {sorted(unknown)!r}; "
144
- f"allowed: {sorted(_RANK_LIST_NAMES)!r}"
145
- )
146
- if not isinstance(self.rrf_k, int) or self.rrf_k < 1:
147
- raise ValueError(f"RankConfig.rrf_k must be an int >= 1, got {self.rrf_k!r}")
148
-
149
-
150
- # Production default: ship the 3-list config (vector+graph+bm25). The BM25 list
151
- # is wired in ``search_lancedb._graph_expand_merge``; on installs without the
152
- # vector stack, or when the FTS index is unavailable, it degrades silently to
153
- # the 2-list (vector+graph) fusion.
154
- DEFAULT_RANK_CONFIG = RankConfig(lists=frozenset({"vector", "graph", "bm25"}), rrf_k=60)
155
-
156
- # Eval convenience: the historical 2-list (vector+graph) fusion, used by
157
- # evaluation harnesses that isolate the vector+graph baseline from the bm25 list.
158
- BASELINE_2LIST_CONFIG = RankConfig(lists=frozenset({"vector", "graph"}), rrf_k=60)
159
-
160
-
161
- # Theoretical maximum for hybrid composite score (used for display normalization).
162
- # Hybrid sort metric: raw_rrf * (import_factor if import_heavy else 1)
163
- # + role_weight + symbol_bonus
164
- # where raw_rrf ≤ 2/(k+1) for 2-list RRF, role_weight ≤ max(_ROLE_SCORE_WEIGHTS),
165
- # and symbol_bonus ≤ _SYMBOL_MATCH_BONUS_CAP + _TYPE_MATCH_BONUS_CAP + _ACTION_VERB_BONUS.
166
- # The import factor is ≤ 1, so we use the raw max (derived via _rrf_max(2)).
167
- _HYBRID_SCORE_MAX = _rrf_max(2) + max(_ROLE_SCORE_WEIGHTS.values()) + _SYMBOL_MATCH_BONUS_CAP + _TYPE_MATCH_BONUS_CAP + _ACTION_VERB_BONUS
168
-
169
-
170
- def _query_tokens(query: str) -> set[str]:
171
- """Lowercased alpha-only tokens from the query, minus stopwords, len >= 3.
172
-
173
- Used to score symbol-name overlap; we keep it simple and locale-free.
174
- """
175
- out: set[str] = set()
176
- cur: list[str] = []
177
-
178
- def _flush() -> None:
179
- if cur:
180
- tok = "".join(cur).lower()
181
- cur.clear()
182
- if len(tok) >= 3 and tok not in _STOPWORDS:
183
- out.add(tok)
184
-
185
- for c in query:
186
- if c.isalpha():
187
- cur.append(c)
188
- else:
189
- _flush()
190
- _flush()
191
- return out
192
-
193
-
194
- def _split_identifier(name: str) -> list[str]:
195
- """camelCase / snake_case -> lowercase token list."""
196
- parts: list[str] = []
197
- cur: list[str] = []
198
- for c in name:
199
- if c == "_":
200
- if cur:
201
- parts.append("".join(cur).lower())
202
- cur = []
203
- elif c.isupper() and cur:
204
- parts.append("".join(cur).lower())
205
- cur = [c]
206
- else:
207
- cur.append(c)
208
- if cur:
209
- parts.append("".join(cur).lower())
210
- return [p for p in parts if p]
211
-
212
-
213
- # Alphanumeric-word extractor for FTS query building (mirrors the index side, which
214
- # runs the same regex over name/fqn/signature/annotations/capabilities/package fields).
215
- _FTS_WORD_RE = re.compile(r"[A-Za-z][A-Za-z0-9]*")
216
-
217
-
218
- def build_fts_query(text: str) -> str:
219
- """Tokenize a search query into the ``Symbol.search_text`` token space (fork A).
220
-
221
- ``search_text`` is indexed from ``_split_identifier`` tokens (camelCase / snake_case
222
- split, lowercased). LadybugDB FTS's own tokenizer does NOT split camelCase, so a raw
223
- pasted identifier like ``DistributionChunkService`` would match nothing — this
224
- extracts alphanumeric words from the query and splits each via ``_split_identifier``
225
- so the query lands in the index's token space. Tokens shorter than 2 chars are
226
- dropped (mirrors the index side); duplicates collapse. Returns ``""`` for a query
227
- with no usable tokens — the caller then falls back to the heuristic / role listing.
228
- """
229
- out: list[str] = []
230
- seen: set[str] = set()
231
- for word in _FTS_WORD_RE.findall(text or ""):
232
- for tok in _split_identifier(word):
233
- if len(tok) >= 2 and tok not in seen:
234
- seen.add(tok)
235
- out.append(tok)
236
- return " ".join(out)
237
-
238
-
239
- def _symbol_bonus(r: dict, query_toks: set[str]) -> float:
240
- """Symbol-name overlap + action-verb bump for java chunks.
241
-
242
- Caps at `_SYMBOL_MATCH_BONUS_CAP + _ACTION_VERB_BONUS` to avoid runaway
243
- ranks on chunks declaring many symbols.
244
- """
245
- if str(r.get("_kind", "")) != "java":
246
- return 0.0
247
- raw = r.get("symbols") or []
248
- if isinstance(raw, str):
249
- # Legacy JSON-encoded list column; parse defensively.
250
- try:
251
- parsed = json.loads(raw)
252
- raw = parsed if isinstance(parsed, list) else []
253
- except Exception:
254
- raw = []
255
- symbols = [str(s) for s in raw if s]
256
-
257
- overlap_hits = 0
258
- has_action = False
259
- for s in symbols:
260
- bare = s.split("(", 1)[0].strip()
261
- if not bare:
262
- continue
263
- toks = _split_identifier(bare)
264
- if toks:
265
- if toks[0] in _ACTION_VERB_PREFIXES:
266
- has_action = True
267
- if query_toks & set(toks):
268
- overlap_hits += 1
269
-
270
- bonus = min(overlap_hits * _SYMBOL_MATCH_BONUS_PER_HIT, _SYMBOL_MATCH_BONUS_CAP)
271
- if has_action:
272
- bonus += _ACTION_VERB_BONUS
273
-
274
- # Type-name overlap: strongest single lexical signal for "which class is
275
- # the answer?" queries. Uses the simple name of primary_type_fqn.
276
- fqn = str(r.get("primary_type_fqn") or "")
277
- if fqn:
278
- simple = fqn.rsplit(".", 1)[-1]
279
- type_toks = set(_split_identifier(simple))
280
- type_hits = len(query_toks & type_toks)
281
- if type_hits:
282
- bonus += min(type_hits * _TYPE_MATCH_BONUS_PER_HIT, _TYPE_MATCH_BONUS_CAP)
283
- return bonus
284
-
285
-
286
- def _role_weight(r: dict) -> float:
287
- """Effective role weight for a row, captured into `_score_components.role_weight`."""
288
- comps = r.setdefault("_score_components", {})
289
- cached = comps.get("role_weight")
290
- if cached is not None:
291
- return float(cached)
292
- if r.get("_skip_role_weight") or str(r.get("_kind", "")) != "java":
293
- comps["role_weight"] = 0.0
294
- return 0.0
295
- role = (r.get("role") or "").upper()
296
- w = _ROLE_SCORE_WEIGHTS.get(role, 0.0)
297
- comps["role_weight"] = w
298
- return w
299
-
300
-
301
- def _apply_symbol_bonus(rows: list[dict], query_toks: set[str]) -> None:
302
- """Pre-compute symbol-match bonus into `_score_components.symbol_bonus`."""
303
- if not query_toks:
304
- return
305
- for r in rows:
306
- if r.get("_skip_role_weight"):
307
- # When the caller locked role, respect their intent everywhere.
308
- continue
309
- b = _symbol_bonus(r, query_toks)
310
- if b:
311
- r.setdefault("_score_components", {})["symbol_bonus"] = b
312
-
313
-
314
- def l2_distance_to_score(distance: float) -> float:
315
- """Map L2 distance to a similarity score for unit-normalized embeddings."""
316
- return 1.0 - distance * distance / 2.0
317
-
318
-
319
- # Display-score denominator for the vector backend. Unit-normalized embeddings
320
- # have L2 distance in [0, 2]; the cosine map ``l2_distance_to_score`` (1 - d²/2)
321
- # goes NEGATIVE past √2 ≈ 1.414 and clamps to 0. Weak-but-best semantic matches
322
- # (e.g. a lone keyword like "controller") commonly sit at d ≈ 1.5, so EVERY hit
323
- # clamps to score=0.000 even though the ranking is correct. ``vector_display_score``
324
- # instead normalizes the effective (bonus-adjusted) distance over the full
325
- # unit-embedding range, so a top-ranked hit stays visibly non-zero. Role/symbol
326
- # bonuses reduce the effective distance and so raise the displayed score,
327
- # keeping it rank-monotonic with the distance-based sort key.
328
- _VECTOR_DISTANCE_REF = 2.0
329
-
330
-
331
- def vector_display_score(effective_distance: float) -> float:
332
- """Displayed vector score in [0, 1] from the effective (bonus-adjusted) distance.
333
-
334
- Bounded linear normalization over the unit-embedding L2 range [0, 2]: lower
335
- distance → higher score. Unlike ``l2_distance_to_score`` (which goes
336
- negative past √2 and clamps a correctly-ranked top hit to 0.000), this keeps
337
- a top result visibly non-zero while staying rank-monotonic with the sort key.
338
- """
339
- return _clamp01(1.0 - effective_distance / _VECTOR_DISTANCE_REF)
340
-
341
-
342
- def _effective_distance(comps: dict[str, float]) -> float:
343
- """Compute the adjusted distance used for sorting.
344
-
345
- Matches _vector_sort_key logic: distance + import_penalty - role_weight - symbol_bonus.
346
- """
347
- d = comps.get("distance", 0.0)
348
- d += comps.get("import_penalty", 0.0)
349
- d -= comps.get("role_weight", 0.0)
350
- d -= comps.get("symbol_bonus", 0.0)
351
- return d
352
-
353
-
354
- def _clamp01(x: float) -> float:
355
- """Clamp a value to the [0.0, 1.0] range."""
356
- if x < 0.0:
357
- return 0.0
358
- if x > 1.0:
359
- return 1.0
360
- return x
361
-
362
-
363
- # Matches a Java top-level type declaration and captures its simple name. Mirrors
364
- # the heuristic in ``ast.chunk_heuristics._JAVA_TYPE`` but is duplicated here so
365
- # this module stays dependency-free (importable on graph-only Intel installs).
366
- _JAVA_TYPE_DECL_RE = re.compile(
367
- r"\b(?:public\s+|private\s+|protected\s+|sealed\s+|non-sealed\s+|final\s+|"
368
- r"abstract\s+|static\s+)*"
369
- r"(?:class|interface|enum|record)\s+([A-Za-z_][A-Za-z0-9_]*)"
370
- )
371
-
372
-
373
- def declaration_line_number(
374
- text: str | None, anchor_line: int | None, type_name: str | None = None
375
- ) -> int | None:
376
- """Absolute 1-based line of the Java type declaration within chunk ``text``.
377
-
378
- LanceDB chunks are anchored at the chunk's first source line, which for a
379
- file-spanning chunk is the package/import line (``anchor_line`` = 1) while
380
- the ``class``/``interface`` declaration sits several lines down. Without
381
- this, hits render as ``File.java:1`` even though the symbol is declared
382
- later (F8). Returns ``anchor_line + i`` for the first matching declaration
383
- (pinned to ``type_name`` when given, so a nested type doesn't win), or
384
- ``anchor_line`` unchanged when no declaration is found in the chunk.
385
-
386
- Comment-aware: Javadoc/line/block-comment lines that merely MENTION the type
387
- name (e.g. ``* This class Bar handles...``) are skipped so the returned line
388
- is the real declaration, not a comment above it.
389
- """
390
- if not text or anchor_line is None:
391
- return anchor_line
392
- in_block = False
393
- for i, raw in enumerate(text.splitlines()):
394
- # Drop a trailing ``// ...`` line comment before any matching (a ``//``
395
- # inside a string literal is unrealistic for a declaration line).
396
- code = raw.split("//", 1)[0]
397
- stripped = code.strip()
398
- if in_block:
399
- if "*/" in stripped:
400
- in_block = False
401
- continue
402
- if stripped.startswith("/*"):
403
- # Single-line ``/* ... */`` -> skip without entering block state.
404
- if "*/" not in stripped[2:]:
405
- in_block = True
406
- continue
407
- if not stripped or stripped.startswith("*"):
408
- # Blank or a Javadoc continuation line (`` * ...``).
409
- continue
410
- m = _JAVA_TYPE_DECL_RE.search(code)
411
- if m and (not type_name or m.group(1) == type_name):
412
- return anchor_line + i
413
- return anchor_line
414
-
415
-
416
- def explain_score_components(
417
- comps: dict[str, float] | None,
418
- *,
419
- role: str | None = None,
420
- hybrid: bool = False,
421
- graph_expanded: bool = False,
422
- lexical: bool = False,
423
- ) -> str:
424
- """Compact human-readable 'why' string for a ranked hit.
425
-
426
- Joins the interesting components of `_score_components` in a stable order
427
- so agents can reason about rankings without chasing raw floats. Returns
428
- "" if there's nothing worth mentioning.
429
- """
430
- if not comps:
431
- comps = {}
432
- parts: list[str] = []
433
- if lexical:
434
- rel = comps.get("lexical_relevance")
435
- if rel is not None:
436
- parts.append(f"relevance={float(rel):.2f}")
437
- nm = comps.get("name_match")
438
- if nm is not None:
439
- parts.append(f"name={float(nm):.2f}")
440
- ty = comps.get("type_match")
441
- if ty:
442
- parts.append(f"type:{float(ty):+.2f}")
443
- fq = comps.get("fqn_match")
444
- if fq:
445
- parts.append(f"fqn:{float(fq):+.2f}")
446
- elif hybrid:
447
- # Prefer rrf_raw (added by PR-SEARCH-1a) for explanation
448
- rrf = comps.get("rrf_raw") or comps.get("hybrid_rrf")
449
- if rrf is not None:
450
- parts.append(f"rrf={float(rrf):.3f}")
451
- bm25 = comps.get("bm25")
452
- if bm25:
453
- parts.append(f"bm25={float(bm25):.3f}")
454
- else:
455
- d = comps.get("distance")
456
- if d is not None:
457
- parts.append(f"dist={float(d):.2f}")
458
- rw = comps.get("role_weight")
459
- if rw:
460
- label = f"role:{role}" if role else "role"
461
- parts.append(f"{label}:{float(rw):+.02f}")
462
- sb = comps.get("symbol_bonus")
463
- if sb:
464
- parts.append(f"symbol:{float(sb):+.02f}")
465
- ip = comps.get("import_penalty")
466
- if ip:
467
- parts.append(f"import_penalty:{float(ip):+.02f}")
468
- if graph_expanded:
469
- parts.append("graph")
470
- return " ".join(parts)
471
-
472
-
473
- def _dedup_by_fqn(rows: list[dict], dedup_by_fqn: bool = True) -> list[dict]:
474
- """Deduplicate rows by primary_type_fqn (java table only).
475
-
476
- When dedup_by_fqn is True, collapses multiple chunks of the same
477
- primary_type_fqn into one row (first-seen-wins, since rows are pre-sorted
478
- so the first is the best chunk). Each survivor gets a _chunks_collapsed
479
- field (>=1) counting how many rows were collapsed into it.
480
-
481
- Rows without primary_type_fqn (sql/yaml tables) get a unique __id:<id>
482
- key so they pass through unchanged (each row is unique).
483
-
484
- When dedup_by_fqn is False, returns rows unchanged (regression guard).
485
- """
486
- if not dedup_by_fqn:
487
- # Non-dedup path: return unchanged, byte-identical to prior behavior
488
- return rows
489
-
490
- deduped: list[dict] = []
491
- seen_keys: dict[str, dict] = {}
492
- collapsed_counts: dict[str, int] = {}
493
-
494
- for row in rows:
495
- # Build dedup key: primary_type_fqn for java rows, unique __id:<id> for sql/yaml
496
- fqn = row.get("primary_type_fqn")
497
- if fqn:
498
- key = str(fqn)
499
- else:
500
- # sql/yaml rows have no primary_type_fqn → unique key per row
501
- row_id = row.get("id") or id(row)
502
- key = f"__id:{row_id}"
503
-
504
- if key not in seen_keys:
505
- # First occurrence: keep it
506
- seen_keys[key] = row
507
- collapsed_counts[key] = 1
508
- deduped.append(row)
509
- else:
510
- # Duplicate: increment collapse count, discard this row
511
- collapsed_counts[key] += 1
512
-
513
- # Annotate each survivor with _chunks_collapsed
514
- for row in deduped:
515
- fqn = row.get("primary_type_fqn")
516
- if fqn:
517
- key = str(fqn)
518
- else:
519
- row_id = row.get("id") or id(row)
520
- key = f"__id:{row_id}"
521
- row["_chunks_collapsed"] = collapsed_counts[key]
522
-
523
- return deduped
File without changes