sourcecode 1.5.0__py3-none-any.whl → 1.7.0__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.
sourcecode/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  """sourcecode — Deterministic codebase context maps for AI coding agents."""
2
2
 
3
- __version__ = "1.5.0"
3
+ __version__ = "1.7.0"
@@ -135,7 +135,8 @@ class AdaptiveScanner:
135
135
  if current_depth >= n:
136
136
  # At or inside the source root
137
137
  if rel_parts[:n] == src_parts:
138
- return src_max # definite source territoryearly exit
138
+ # base_depth acts as a floor explicit --depth always wins
139
+ return max(src_max, self.base_depth)
139
140
  else:
140
141
  # Ancestor check: src_parts starts with rel_parts?
141
142
  if src_parts[:current_depth] == rel_parts:
sourcecode/cli.py CHANGED
@@ -10,6 +10,7 @@ import typer
10
10
 
11
11
  from sourcecode import __version__
12
12
  from sourcecode.entrypoint_classifier import is_production_entry_point, normalize_entry_point
13
+ from sourcecode.progress import Progress
13
14
 
14
15
 
15
16
  # ---------------------------------------------------------------------------
@@ -138,18 +139,19 @@ def _check_pipeline_coherence(sm: "SourceMap") -> list[str]: # type: ignore[nam
138
139
  return issues
139
140
 
140
141
  _HELP = """\
141
- Deterministic codebase context for AI coding agents.
142
+ Compressed AI-ready context for Java/Spring enterprise codebases.
142
143
 
143
- [bold]Usage:[/bold]
144
- sourcecode [dim]# analyze current directory[/dim]
145
- sourcecode /path/to/repo [dim]# analyze specific path[/dim]
146
- sourcecode --agent [dim]# structured output for AI agents[/dim]
144
+ [bold]Examples:[/bold]
145
+ sourcecode saint-server --compact
146
+ sourcecode . --changed-only --git-context
147
+ sourcecode saint-server --symbol SeguridadRestController
148
+ sourcecode prepare-context onboard saint-server
149
+ sourcecode prepare-context delta . --since main
147
150
 
148
151
  [bold]Subcommands:[/bold]
149
- prepare-context TASK [PATH] [dim]# task-specific context[/dim]
152
+ prepare-context TASK [PATH] [dim]# task-specific context (onboard, delta, fix-bug, ...)[/dim]
150
153
  telemetry status|enable|disable
151
154
  version
152
- config
153
155
  """
154
156
 
155
157
  # Known subcommand names — tokens matching these are routed as subcommands,
@@ -382,28 +384,28 @@ def main(
382
384
  graph_modules: bool = typer.Option(
383
385
  False,
384
386
  "--graph-modules",
385
- help=(
386
- "Include a structural module graph: nodes (files/symbols) and edges (imports, calls, contains). "
387
- "Useful for understanding coupling and call flows. Adds module_graph to output. "
388
- "Combine with --graph-detail and --graph-edges to control scope."
389
- ),
387
+ hidden=True,
388
+ help="Include a structural module graph in output.",
390
389
  ),
391
390
  graph_detail: str = typer.Option(
392
391
  "high",
393
392
  "--graph-detail",
394
- help="Detail level for --graph-modules: high (top modules by importance), medium (filtered by relevance), full (all nodes and edges). Default: high.",
393
+ hidden=True,
394
+ help="Detail level for --graph-modules: high, medium, full.",
395
395
  show_default=True,
396
396
  ),
397
397
  max_nodes: Optional[int] = typer.Option(
398
398
  None,
399
399
  "--max-nodes",
400
- help="Maximum number of nodes in --graph-modules output when using high or medium detail. Prevents oversized graphs in large codebases.",
400
+ hidden=True,
401
+ help="Maximum nodes in --graph-modules output.",
401
402
  min=1,
402
403
  ),
403
404
  graph_edges: Optional[str] = typer.Option(
404
405
  None,
405
406
  "--graph-edges",
406
- help="Edge types for --graph-modules, comma-separated: imports,calls,contains,extends. Default: all available. Example: --graph-edges imports,calls",
407
+ hidden=True,
408
+ help="Edge types for --graph-modules, comma-separated: imports,calls,contains,extends.",
407
409
  ),
408
410
  no_tree: bool = typer.Option(
409
411
  False,
@@ -414,10 +416,8 @@ def main(
414
416
  tree: bool = typer.Option(
415
417
  False,
416
418
  "--tree",
417
- help=(
418
- "Include the full file_tree and flat file_paths list in output (deep-dive layer). "
419
- "Adds significant size — use when the agent needs to browse the full file structure."
420
- ),
419
+ hidden=True,
420
+ help="Include the full file_tree and flat file_paths list in output.",
421
421
  ),
422
422
  no_redact: bool = typer.Option(
423
423
  False,
@@ -448,41 +448,33 @@ def main(
448
448
  docs: bool = typer.Option(
449
449
  False,
450
450
  "--docs",
451
- help="Extract documentation: docstrings, function signatures, and module-level comments. Adds doc_summary and docs to output. Combine with --docs-depth to control coverage.",
451
+ hidden=True,
452
+ help="Extract documentation: docstrings, function signatures, and module-level comments.",
452
453
  ),
453
454
  docs_depth: str = typer.Option(
454
455
  "symbols",
455
456
  "--docs-depth",
456
- help="Documentation extraction depth: module (module-level only), symbols (functions and classes), full (all symbols including private). Default: symbols.",
457
+ hidden=True,
458
+ help="Documentation extraction depth: module, symbols, full.",
457
459
  show_default=True,
458
460
  ),
459
461
  full_metrics: bool = typer.Option(
460
462
  False,
461
463
  "--full-metrics",
462
- help=(
463
- "Technical audit: lines of code, symbol counts, cyclomatic complexity, and test coverage per file. "
464
- "Produces file_metrics and metrics_summary. "
465
- "Not included in --agent output — designed for CI pipelines and code review tools, not as primary agent context."
466
- ),
464
+ hidden=True,
465
+ help="Technical audit: LOC, complexity, test coverage per file.",
467
466
  ),
468
467
  semantics: bool = typer.Option(
469
468
  False,
470
469
  "--semantics",
471
- help=(
472
- "Semantic analysis: cross-file symbol resolution, call graph with confidence levels, and import linking. "
473
- "Adds semantic_calls, semantic_symbols, semantic_links, semantic_summary, and hotspots (files ranked by fan-in/fan-out). "
474
- "Slower than default analysis — skip for quick scans. "
475
- "Confidence degrades on dynamic dispatch, decorators, and generated code."
476
- ),
470
+ hidden=True,
471
+ help="Cross-file symbol resolution and call graph analysis.",
477
472
  ),
478
473
  architecture: bool = typer.Option(
479
474
  False,
480
475
  "--architecture",
481
- help=(
482
- "Architectural inference: detect functional layers (MVC/layered/hexagonal), bounded contexts, "
483
- "and dominant structural patterns. Adds architecture to output. "
484
- "Confidence is low when based on directory names alone — combine with --semantics for higher accuracy."
485
- ),
476
+ hidden=True,
477
+ help="Architectural layer inference (MVC/hexagonal/layered).",
486
478
  ),
487
479
  git_context: bool = typer.Option(
488
480
  False,
@@ -788,7 +780,7 @@ def main(
788
780
  # Require at least 8: src(1)+main(2)+java(3)+com(4)+co(5)+app(6)+module(7)+file.
789
781
  _java_manifest_names = {"pom.xml", "build.gradle", "build.gradle.kts"}
790
782
  _is_java = any(Path(m).name in _java_manifest_names for m in manifests)
791
- _java_min_depth = 10
783
+ _java_min_depth = 12
792
784
  effective_depth = max(depth, _java_min_depth) if _is_java and depth < _java_min_depth else depth
793
785
 
794
786
  # --agent: enable signal analyzers; output via agent_view (not compact)
@@ -1399,11 +1391,30 @@ def main(
1399
1391
  ))
1400
1392
  sm = _replace(sm, pipeline_trace=_trace.build_trace())
1401
1393
 
1402
- # P3-B: Auto-switch to centrality ranking when DDD layout detected
1403
- if (rank_by == "relevance"
1404
- and sm.architecture is not None
1405
- and sm.architecture.pattern == "ddd"):
1406
- rank_by = "centrality"
1394
+ # Pre-compute uncommitted files for --changed-only.
1395
+ # The contract pipeline filter and git_context are two separate subsystems;
1396
+ # wire them here so the pipeline uses git_context data, not an independent git call.
1397
+ _allowed_changed_files: Optional[set[str]] = None
1398
+ if changed_only:
1399
+ from sourcecode.git_analyzer import GitAnalyzer as _GitAnalyzerEarly
1400
+ try:
1401
+ _gc_early = _GitAnalyzerEarly().analyze(target, depth=1, days=1)
1402
+ _bad_gc = {"no_git_repo", "git_not_found", "git_timeout"}
1403
+ if _gc_early and not (_bad_gc & set(_gc_early.limitations)):
1404
+ _uc = _gc_early.uncommitted_changes
1405
+ if _uc:
1406
+ _allowed_changed_files = (
1407
+ set(_uc.staged) | set(_uc.unstaged) | set(_uc.untracked)
1408
+ )
1409
+ if not _allowed_changed_files:
1410
+ typer.echo(
1411
+ "[changed-only] git unavailable or no uncommitted changes — falling back to full scan.",
1412
+ err=True,
1413
+ )
1414
+ changed_only = False
1415
+ except Exception:
1416
+ typer.echo("[changed-only] git error — falling back to full scan.", err=True)
1417
+ changed_only = False
1407
1418
 
1408
1419
  # Contract pipeline — runs for mode=contract|standard|deep|hybrid (skip for raw)
1409
1420
  _is_contract_mode = mode in ("contract", "standard")
@@ -1438,6 +1449,7 @@ def main(
1438
1449
  max_importers=max_importers,
1439
1450
  semantic_calls=sm.semantic_calls or None,
1440
1451
  code_notes=sm.code_notes or None,
1452
+ allowed_changed_files=_allowed_changed_files,
1441
1453
  **_java_pipeline_kwargs,
1442
1454
  )
1443
1455
  except Exception as _exc:
@@ -1712,8 +1724,13 @@ def prepare_context_cmd(
1712
1724
  if llm_prompt:
1713
1725
  out["llm_prompt"] = builder.render_prompt(output)
1714
1726
 
1727
+ import sys as _sys
1715
1728
  _pc_content = json.dumps(out, indent=2, ensure_ascii=False)
1716
- typer.echo(_pc_content)
1729
+ _pc_bytes = _pc_content.encode("utf-8")
1730
+ _sys.stdout.buffer.write(_pc_bytes)
1731
+ if not _pc_content.endswith("\n"):
1732
+ _sys.stdout.buffer.write(b"\n")
1733
+ _sys.stdout.buffer.flush()
1717
1734
 
1718
1735
  if copy:
1719
1736
  _trimmed = _pc_content.strip()
@@ -183,6 +183,7 @@ class ContractPipeline:
183
183
  code_notes: Optional[list] = None,
184
184
  max_contracts: Optional[int] = _MAX_CONTRACTS,
185
185
  min_score: Optional[float] = None,
186
+ allowed_changed_files: Optional[set[str]] = None,
186
187
  ) -> tuple[list[FileContract], ContractSummary]:
187
188
  """Run the full extraction pipeline.
188
189
 
@@ -200,9 +201,14 @@ class ContractPipeline:
200
201
  engine = RankingEngine(monorepo_packages)
201
202
 
202
203
  # 1. Changed files (for --changed-only and ranking)
204
+ # When allowed_changed_files is provided (from pre-computed git uncommitted_changes),
205
+ # use it directly. Otherwise fall back to independent git commands.
203
206
  changed_files: set[str] = set()
204
207
  if changed_only or rank_by == "git-churn":
205
- changed_files = _get_changed_files(root)
208
+ if allowed_changed_files is not None:
209
+ changed_files = allowed_changed_files
210
+ else:
211
+ changed_files = _get_changed_files(root)
206
212
 
207
213
  # 2. Select files to extract
208
214
  # Exclude test files by default — they dominate by count but add noise
@@ -240,10 +246,18 @@ class ContractPipeline:
240
246
 
241
247
  # Apply max_files cap — bypass when symbol search to ensure defining files are found.
242
248
  # A symbol query over a large repo needs all files; result set is small after filtering.
249
+ # MyBatis Mapper.xml contracts rank below Java files on path score alone (.xml has no
250
+ # suffix boost). Give them the same priority slot as entry_points so they survive the cap.
251
+ def _is_priority(p: str) -> bool:
252
+ if p in entry_paths:
253
+ return True
254
+ name = p.rsplit("/", 1)[-1]
255
+ return name.lower().endswith("mapper.xml")
256
+
243
257
  if symbol is None and len(src_paths) > self.max_files:
244
258
  src_paths = sorted(
245
259
  src_paths,
246
- key=lambda p: (p in entry_paths, scorer.score(p)),
260
+ key=lambda p: (_is_priority(p), scorer.score(p)),
247
261
  reverse=True,
248
262
  )[:self.max_files]
249
263
 
@@ -362,7 +376,9 @@ class ContractPipeline:
362
376
 
363
377
  def _rank(self, contracts: list[FileContract], rank_by: RankStrategy) -> list[FileContract]:
364
378
  if rank_by == "centrality":
365
- return sorted(contracts, key=lambda c: (-(c.fan_in + c.fan_out), c.path))
379
+ # Entrypoints (REST controllers, main classes) surface first even in centrality mode:
380
+ # they have low fan_in (not imported) but are the primary API surface.
381
+ return sorted(contracts, key=lambda c: (-c.is_entrypoint, -(c.fan_in + c.fan_out), c.path))
366
382
  if rank_by == "git-churn":
367
383
  return sorted(contracts, key=lambda c: (-c.is_changed, -c.relevance_score, c.path))
368
384
  # Default: relevance — path breaks ties deterministically
@@ -225,6 +225,12 @@ class JavaDetector(AbstractDetector):
225
225
  all_paths = flatten_file_tree(context.file_tree)
226
226
  all_java = [p for p in all_paths if p.endswith(".java")]
227
227
 
228
+ # Augment with a direct scan of standard Java source roots for Controller-named
229
+ # files that the depth-limited file_tree scanner may have missed.
230
+ # DDD layouts place REST controllers at depth 10+ (e.g.
231
+ # src/main/java/com/org/app/ddd/domain/infraestructure/rest/XxxRestController.java).
232
+ self._augment_deep_java_controllers(context, all_java)
233
+
228
234
  # 1. @SpringBootApplication entry: Application.java / Main.java by name
229
235
  app_candidates = [
230
236
  p for p in all_java
@@ -270,6 +276,40 @@ class JavaDetector(AbstractDetector):
270
276
  unique_eps.append(ep)
271
277
  return unique_eps
272
278
 
279
+ def _augment_deep_java_controllers(self, context: DetectionContext, all_java: list[str]) -> None:
280
+ """Scan standard Java source roots for *Controller*.java files not in all_java.
281
+
282
+ The depth-limited file_tree scanner misses files at depth >= max_depth.
283
+ DDD layouts place REST controllers deep (e.g. depth 10+), so we supplement
284
+ with a direct filesystem walk scoped to the standard Maven/Gradle source root.
285
+ """
286
+ import os as _os
287
+ existing = set(all_java)
288
+ # Standard Java source root candidates (Maven first, then Gradle/other)
289
+ _SRC_ROOTS = ("src/main/java", "src/main/kotlin", "src/java", "src")
290
+ for src_root_name in _SRC_ROOTS:
291
+ src_root = context.root / src_root_name
292
+ if not src_root.is_dir():
293
+ continue
294
+ try:
295
+ for dirpath, _dirs, filenames in _os.walk(str(src_root)):
296
+ for fname in filenames:
297
+ if "Controller" not in fname or not fname.endswith(".java"):
298
+ continue
299
+ full = Path(dirpath) / fname
300
+ if full.is_symlink():
301
+ continue
302
+ try:
303
+ rel = str(full.relative_to(context.root)).replace("\\", "/")
304
+ if rel not in existing:
305
+ all_java.append(rel)
306
+ existing.add(rel)
307
+ except ValueError:
308
+ pass
309
+ except OSError:
310
+ pass
311
+ return # use only first matching source root
312
+
273
313
  def _scan_java_file_for_entry_points(self, abs_path: Path, rel_path: str) -> list[EntryPoint]:
274
314
  try:
275
315
  if abs_path.stat().st_size > _MAX_FILE_SIZE:
@@ -398,11 +398,27 @@ class TaskContextBuilder:
398
398
  from sourcecode.tree_utils import flatten_file_tree
399
399
 
400
400
  _topology = RepoClassifier().classify(self.root)
401
- scanner = AdaptiveScanner(self.root, topology=_topology, base_depth=6)
401
+ # Shallow pre-scan to detect Java manifests before choosing depth.
402
+ from sourcecode.scanner import FileScanner as _FileScanner
403
+ _pre = _FileScanner(self.root, max_depth=1)
404
+ _pre_manifests = _pre.find_manifests()
405
+ _java_names = {"pom.xml", "build.gradle", "build.gradle.kts"}
406
+ _is_java = any(Path(m).name in _java_names for m in _pre_manifests)
407
+ _base_depth = 12 if _is_java else 6
408
+ scanner = AdaptiveScanner(self.root, topology=_topology, base_depth=_base_depth)
402
409
  file_tree = scanner.scan_tree()
403
410
  manifests = scanner.find_manifests()
404
411
  all_paths = [p.replace("\\", "/") for p in flatten_file_tree(file_tree)]
405
412
 
413
+ # Warn when Java project has no Mapper.xml — suggests files below scan depth.
414
+ _mybatis_warning: dict | None = None
415
+ if _is_java and not any(p.endswith("Mapper.xml") for p in all_paths):
416
+ _mybatis_warning = {
417
+ "area": "mybatis",
418
+ "reason": "Mapper XML files may exist below scan depth. Re-run with --depth 12.",
419
+ "impact": "high",
420
+ }
421
+
406
422
  # ── 2. Detect stacks + entry points ───────────────────────────────
407
423
  from dataclasses import replace as _replace
408
424
  from sourcecode.detectors import ProjectDetector, build_default_detectors
@@ -500,6 +516,7 @@ class TaskContextBuilder:
500
516
  code_notes_summary: Optional[dict[str, Any]] = None
501
517
  suspected_areas: list[str] = []
502
518
  improvement_opportunities: list[str] = []
519
+ cn_notes_for_ranking: list = []
503
520
 
504
521
  if spec.enable_code_notes:
505
522
  from dataclasses import asdict
@@ -507,6 +524,7 @@ class TaskContextBuilder:
507
524
 
508
525
  cn_notes, _cn_adrs, cn_summary = CodeNotesAnalyzer().analyze(self.root)
509
526
  code_notes_summary = asdict(cn_summary)
527
+ cn_notes_for_ranking = cn_notes
510
528
 
511
529
  if task_name == "fix-bug":
512
530
  bug_kinds = {"FIXME", "BUG", "HACK", "XXX"}
@@ -555,6 +573,7 @@ class TaskContextBuilder:
555
573
  monorepo_packages=sm.monorepo_packages if sm.monorepo_packages else None,
556
574
  git_hotspots=git_hotspots,
557
575
  uncommitted_files=uncommitted_files,
576
+ code_notes=cn_notes_for_ranking if cn_notes_for_ranking else None,
558
577
  )
559
578
 
560
579
  # ── 7. Test gaps (generate-tests only) ────────────────────────────
@@ -594,6 +613,8 @@ class TaskContextBuilder:
594
613
  conf_summary, analysis_gaps = ConfidenceAnalyzer().analyze(sm_for_conf)
595
614
  confidence = conf_summary.overall
596
615
  gaps = [g.reason for g in analysis_gaps]
616
+ if _mybatis_warning:
617
+ gaps.append(_mybatis_warning["reason"])
597
618
 
598
619
  # ── 9. why_these_files ────────────────────────────────────────────────
599
620
  why_these_files: dict[str, str] = {
@@ -705,6 +726,7 @@ class TaskContextBuilder:
705
726
  monorepo_packages: Optional[list] = None,
706
727
  git_hotspots: Optional[dict[str, int]] = None,
707
728
  uncommitted_files: Optional[set[str]] = None,
729
+ code_notes: Optional[list] = None,
708
730
  ) -> list[RelevantFile]:
709
731
  from sourcecode.ranking_engine import RankingEngine
710
732
  from sourcecode.file_classifier import FileClassifier
@@ -719,6 +741,35 @@ class TaskContextBuilder:
719
741
  _uncommitted = uncommitted_files or set()
720
742
  _max_churn = max(_hotspots.values(), default=1)
721
743
 
744
+ # Pre-compute fix-bug signals (used only when task_name == "fix-bug")
745
+ _annotated_files: set[str] = set()
746
+ _dominant_stack = ""
747
+ _recently_changed_stacks: set[str] = set()
748
+ if task_name == "fix-bug":
749
+ _bug_kinds = {"FIXME", "BUG"}
750
+ for _n in (code_notes or []):
751
+ if getattr(_n, "kind", "").upper() in _bug_kinds:
752
+ _annotated_files.add(getattr(_n, "path", ""))
753
+
754
+ def _file_stack(p: str) -> str:
755
+ ext = Path(p).suffix.lower()
756
+ if ext == ".java": return "java"
757
+ if ext in (".ts", ".tsx", ".js", ".jsx", ".mjs"): return "typescript"
758
+ if ext == ".py": return "python"
759
+ if ext == ".go": return "go"
760
+ if ext in (".kt", ".kts"): return "kotlin"
761
+ if ext == ".rs": return "rust"
762
+ if ext == ".rb": return "ruby"
763
+ return "unknown"
764
+
765
+ from collections import Counter as _Counter
766
+ _stk_counts: _Counter[str] = _Counter(
767
+ _file_stack(f) for f in _uncommitted if _file_stack(f) != "unknown"
768
+ )
769
+ if _stk_counts:
770
+ _dominant_stack = _stk_counts.most_common(1)[0][0]
771
+ _recently_changed_stacks = set(_stk_counts.keys())
772
+
722
773
  scored: list[tuple[float, str, RelevantFile]] = []
723
774
 
724
775
  for path in all_paths:
@@ -762,10 +813,28 @@ class TaskContextBuilder:
762
813
 
763
814
  # Task-specific boosts for differentiated file weighting
764
815
  path_lower = path.lower()
816
+ _fix_bug_why = ""
765
817
  if task_name == "fix-bug":
766
- if any(x in path_lower for x in ("exception", "error", "handler", "advice")):
767
- content_boost += 1.5
768
- content_reasons.append("exception handler — high risk area")
818
+ _why_parts: list[str] = []
819
+ if path in _uncommitted:
820
+ content_boost += 0.40
821
+ _why_parts.append("uncommitted change (+0.40)")
822
+ _recency = min(0.30, _hotspots.get(path, 0) * 0.05)
823
+ if _recency > 0:
824
+ content_boost += _recency
825
+ _why_parts.append(f"recent commits (+{_recency:.2f})")
826
+ if path in _annotated_files:
827
+ content_boost += 0.20
828
+ _why_parts.append("FIXME/BUG annotation (+0.20)")
829
+ _file_stk = _file_stack(path)
830
+ if _dominant_stack and _file_stk == _dominant_stack:
831
+ content_boost += 0.10
832
+ _why_parts.append("dominant changed stack (+0.10)")
833
+ if _recently_changed_stacks and _file_stk not in _recently_changed_stacks and _file_stk != "unknown":
834
+ content_boost -= 0.30
835
+ _why_parts.append("different stack from recent changes (-0.30)")
836
+ if _why_parts:
837
+ _fix_bug_why = ", ".join(_why_parts)
769
838
  elif task_name == "generate-tests":
770
839
  stem = Path(path).stem.lower()
771
840
  has_test = any(
@@ -797,7 +866,7 @@ class TaskContextBuilder:
797
866
  )
798
867
  all_reasons = [r for r in fs.reasons if r != "source file"] + content_reasons
799
868
  reason_str = ", ".join(all_reasons) if all_reasons else "source file"
800
- why_str = _java_why(path, file_class)
869
+ why_str = _fix_bug_why if _fix_bug_why else _java_why(path, file_class)
801
870
 
802
871
  scored.append((total, path, RelevantFile(
803
872
  path=path,
sourcecode/progress.py ADDED
@@ -0,0 +1,110 @@
1
+ """Lightweight terminal progress indicator for long-running commands.
2
+
3
+ Writes only to stderr. Zero-cost when stderr is not a TTY or CI is detected.
4
+ Thread-safe; stop() is idempotent.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import os
9
+ import sys
10
+ import threading
11
+ import time
12
+ from typing import Optional
13
+
14
+ _FRAMES = ("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏")
15
+
16
+
17
+ def _terminal_capable() -> bool:
18
+ if not sys.stderr.isatty():
19
+ return False
20
+ if os.environ.get("TERM") in ("dumb", ""):
21
+ return False
22
+ if os.environ.get("NO_COLOR"):
23
+ return False
24
+ if os.environ.get("CI"):
25
+ return False
26
+ return True
27
+
28
+
29
+ class Progress:
30
+ """In-place spinner on stderr.
31
+
32
+ Usage::
33
+
34
+ p = Progress()
35
+ p.start("scanning files")
36
+ ...
37
+ p.update("extracting contracts")
38
+ ...
39
+ p.finish() # clears line, prints "✓ done (3.2s)"
40
+
41
+ Always call stop() or finish() — both are idempotent.
42
+ """
43
+
44
+ def __init__(self) -> None:
45
+ self._enabled = _terminal_capable()
46
+ self._phase = ""
47
+ self._t0 = time.monotonic()
48
+ self._lock = threading.Lock()
49
+ self._stop_event = threading.Event()
50
+ self._thread: Optional[threading.Thread] = None
51
+ self._stopped = False
52
+
53
+ @property
54
+ def elapsed(self) -> float:
55
+ return time.monotonic() - self._t0
56
+
57
+ def start(self, phase: str = "initializing") -> "Progress":
58
+ self._t0 = time.monotonic()
59
+ with self._lock:
60
+ self._phase = phase
61
+ self._stopped = False
62
+ if not self._enabled:
63
+ return self
64
+ self._stop_event.clear()
65
+ self._thread = threading.Thread(target=self._loop, daemon=True)
66
+ self._thread.start()
67
+ return self
68
+
69
+ def update(self, phase: str) -> None:
70
+ with self._lock:
71
+ self._phase = phase
72
+
73
+ def stop(self) -> float:
74
+ """Stop and clear spinner. Returns elapsed seconds. Idempotent."""
75
+ elapsed = self.elapsed
76
+ if self._stopped:
77
+ return elapsed
78
+ self._stopped = True
79
+ if not self._enabled:
80
+ return elapsed
81
+ self._stop_event.set()
82
+ if self._thread:
83
+ self._thread.join(timeout=1.0)
84
+ sys.stderr.write("\r\033[K")
85
+ sys.stderr.flush()
86
+ return elapsed
87
+
88
+ def finish(self) -> None:
89
+ """Stop spinner and print a completion line to stderr."""
90
+ elapsed = self.stop()
91
+ if not self._enabled:
92
+ return
93
+ t = f"{elapsed:.1f}s" if elapsed < 60 else f"{elapsed / 60:.1f}m"
94
+ sys.stderr.write(f"✓ done ({t})\n")
95
+ sys.stderr.flush()
96
+
97
+ def _loop(self) -> None:
98
+ idx = 0
99
+ while not self._stop_event.wait(timeout=0.08):
100
+ frame = _FRAMES[idx % len(_FRAMES)]
101
+ elapsed = time.monotonic() - self._t0
102
+ with self._lock:
103
+ phase = self._phase
104
+ line = f"\r{frame} {phase} ({elapsed:.1f}s)"
105
+ try:
106
+ sys.stderr.write(line)
107
+ sys.stderr.flush()
108
+ except Exception:
109
+ break
110
+ idx += 1
sourcecode/serializer.py CHANGED
@@ -1110,6 +1110,10 @@ def standard_view(sm: SourceMap, *, include_tree: bool = False) -> dict[str, Any
1110
1110
 
1111
1111
  if sm.semantic_summary is not None and sm.semantic_summary.requested:
1112
1112
  result["semantic_summary"] = asdict(sm.semantic_summary)
1113
+ # Backward compat: also emit hotspots at top level (moved to semantic_summary in v1.5.0).
1114
+ # Consumers reading d["hotspots"] directly still work.
1115
+ if sm.semantic_summary.hotspots:
1116
+ result["hotspots"] = sm.semantic_summary.hotspots[:10]
1113
1117
  # Defensive filter: never emit objects with null required fields.
1114
1118
  # A null entry in these arrays is worse than a shorter array — it causes
1115
1119
  # agents to misinterpret the analysis as valid when it is not.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sourcecode
3
- Version: 1.5.0
3
+ Version: 1.7.0
4
4
  Summary: Deterministic codebase context for AI coding agents
5
5
  License: Apache License
6
6
  Version 2.0, January 2004
@@ -1,16 +1,16 @@
1
- sourcecode/__init__.py,sha256=81eXvIk2uxc6dFJg9ND6pJ-xePSGbA0ZUJgr1h_bZ48,102
2
- sourcecode/adaptive_scanner.py,sha256=6dh34C2qZXyRbw-8xBhbEwDdXanM6CRFRWayVoYITnA,10190
1
+ sourcecode/__init__.py,sha256=MNhBIzrBdGRgftj8IVZxLq9o3V4ixPrsZuHUqg_wZZw,102
2
+ sourcecode/adaptive_scanner.py,sha256=RTNExwWPXzjgLaRueT7UuxkPj5ZEToWjGbx1j0LSZ9E,10250
3
3
  sourcecode/architecture_analyzer.py,sha256=oPmGPf9_p6y8Z7SIGHEu57nVYwxycIovDzBvlHl-l5k,32557
4
4
  sourcecode/architecture_summary.py,sha256=J9yoLgh8wXwIRrT6q6JooB6PekivbOEYpJz4BUXdalk,20545
5
5
  sourcecode/ast_extractor.py,sha256=XgrZg2DcWcUm9r87cRG3KGO7IK2TIL_N-CvhSbUmmh4,49901
6
6
  sourcecode/classifier.py,sha256=GKTMN8qKZX7ponSwDJfN08RrasI4CVpq1_gFBgEopps,7093
7
- sourcecode/cli.py,sha256=_oYoP4_AAFGw0gg-FBGn_DKh7y49xmwuL6D_Bafl2w4,74497
7
+ sourcecode/cli.py,sha256=LaztdHrrzn4tIrGDmWldYpsPkO1xSU5Wj0ATZBhTyJ8,74423
8
8
  sourcecode/code_notes_analyzer.py,sha256=rRd8bFYV0krjlxxQV0wenwE9K7pVpUQSR7KvSvUQKw4,9226
9
9
  sourcecode/confidence_analyzer.py,sha256=HcaewB2pZaZ_hfKrZWtr_yPMY2-CxS1zzTUD7c4argc,13188
10
10
  sourcecode/context_scorer.py,sha256=QpChSpsmaAYz91rXA4Ue5xzQmNz_ZboZN09YOHScq1U,14679
11
11
  sourcecode/context_summarizer.py,sha256=CiQrfBEzun949bWvmLabWoj2HhPn6Lw62ofqnsy0FlQ,6503
12
12
  sourcecode/contract_model.py,sha256=nRxJKPMs1VHwFTa8AVXhGmaLjti3Lr2sjHDpWgv1bfE,3917
13
- sourcecode/contract_pipeline.py,sha256=_mZn0GD7UjjJ7E79DmmYjF0sVkuOLBZEgZHqgXVyTrs,26648
13
+ sourcecode/contract_pipeline.py,sha256=VEOvmj-emKuUT-GUosde9FRYyeH8szNW1WSnisNSs2o,27592
14
14
  sourcecode/coverage_parser.py,sha256=q0LeZJaX1bnntLu-ImksdBsMlpsVmk_iUfSaB4eaJGo,19702
15
15
  sourcecode/dependency_analyzer.py,sha256=p4ljXhkcGBbFlhaZuPrsjOVjDXaKLTg0Gor2p4qFPP0,56208
16
16
  sourcecode/doc_analyzer.py,sha256=a1CIClCNmfYM3ku4bdgwHQpmb6Js4wdJZ1V5EYLo04I,24345
@@ -20,7 +20,8 @@ sourcecode/file_classifier.py,sha256=48ly5Z6exkzBy8lNy1AkdP4-oJqIA1zT3LZfffuTyDo
20
20
  sourcecode/git_analyzer.py,sha256=PD3eNWydznQ6KLNpxGzBqizIHoPIKevfwz9Xyf_pDt4,11600
21
21
  sourcecode/graph_analyzer.py,sha256=iUK-7pSV-cvGqqD2hENdYmhnm0wcXFEyK-xnu5ul8OU,62515
22
22
  sourcecode/metrics_analyzer.py,sha256=m0ENgtqKeBL17kUIK3fmGkgo7UfXBNHxCMj0H_Y5K7c,22750
23
- sourcecode/prepare_context.py,sha256=LsFDp7HnHdvtwVa46YUD60uMBfwXaVs4suMfBvc8tyI,37357
23
+ sourcecode/prepare_context.py,sha256=8Pk5OMXo6jYfhAFVbpfFsrdqoP0yKQzO9Lk7rj-lmjI,40818
24
+ sourcecode/progress.py,sha256=qn30sWaHOkjTgXsSBmiPkz7Rsbwc5oSlIe6JNEMYp_k,3149
24
25
  sourcecode/ranking_engine.py,sha256=virVglafZufioHpZpwktjMvUiL0TZELWQCQnQNV8dFo,9360
25
26
  sourcecode/redactor.py,sha256=xuGcadGEHaPw4qZXlMDvzMCsr4VOkdp3oBQptHyJk8c,2884
26
27
  sourcecode/relevance_scorer.py,sha256=MYF4FFkveAQps9SmTeTlh6ODiBz2F--_hWNeHMLtUHQ,8405
@@ -29,7 +30,7 @@ sourcecode/runtime_classifier.py,sha256=zWX3r3HCKHc-qtIobErOa8aKMmaoPYREtJKvPcBG
29
30
  sourcecode/scanner.py,sha256=aM3h9-DCQ3xKpeHpHYdo2vX6T5P95HA_YwZbkAVNwmo,8288
30
31
  sourcecode/schema.py,sha256=rNxpDUgOfvJDzz6DKitL-5_0UAh0YomNwTsSSe8PafY,24066
31
32
  sourcecode/semantic_analyzer.py,sha256=12TwXYkYbDcBdu0heX_EmfPM2EkO8a_r5osf0SaeQbs,88956
32
- sourcecode/serializer.py,sha256=2ztWD4FCebPD0D5tY-kNgLlsATaQsPrSQY6rLQeemRI,69467
33
+ sourcecode/serializer.py,sha256=6dHvWc-iqqCjpu9-FcAXqtpKEO4mO3AZVzWT7Tphajo,69736
33
34
  sourcecode/summarizer.py,sha256=ZuzIdm3t8A-d5MuQL0TSNLrd-L0IQIuguIxeNXMNJf8,16070
34
35
  sourcecode/tree_utils.py,sha256=Fj9OIuUksBvgibNd3feog0sMDjVypJzPexp5lvMoYWI,1424
35
36
  sourcecode/workspace.py,sha256=X_6NmNnitvT3_38V-JDChydo_sR68s249hLFlrQskU0,8271
@@ -42,7 +43,7 @@ sourcecode/detectors/elixir.py,sha256=jCpvt5Yi6jvplc80ovRtWh17q-11ZGo9qX7o8b57TJ
42
43
  sourcecode/detectors/go.py,sha256=2r66uRQfeTWsqxr4HDhT6vExZErby0t46QXLHVBRv9w,2782
43
44
  sourcecode/detectors/heuristic.py,sha256=bCqqgbHavl4Sse3dqT8mwmo1wAdgeJr7VyXOmfClLKo,3387
44
45
  sourcecode/detectors/hybrid.py,sha256=IGFRUVsAZ1ooRlFdznCeJAV6vy1yVDx-VyghvLtddXc,9101
45
- sourcecode/detectors/java.py,sha256=0NKsy1uls5wvQSK7DJP_fM-v5_uuMEr_an47caelLHE,16612
46
+ sourcecode/detectors/java.py,sha256=XAnVOmq7yXgO_4UGlIFIVgNRtyWZQEVsDDu3sa5mTsk,18669
46
47
  sourcecode/detectors/jvm_ext.py,sha256=EgHJ5W8EE-ZTN9V607mVzohyKgZE8Mc2jCi-DF8RAZU,2616
47
48
  sourcecode/detectors/nodejs.py,sha256=7fsyAmrGkkguX6U80HUQpIe9MRaYyi_A7zbaRtmFmGc,13097
48
49
  sourcecode/detectors/parsers.py,sha256=ugPg8yNUf0Ai1gA7Fnn6wAkYGFjTxRodSP3IeViYJJ4,2290
@@ -60,8 +61,8 @@ sourcecode/telemetry/consent.py,sha256=wLMvGNJeSSyZoNkQXpoUioY6mMv4Qdvuw7S9jAEWn
60
61
  sourcecode/telemetry/events.py,sha256=oEvvulfsv5GIDWG2174gSS6tNB95w38AIYiYeifGKlE,2294
61
62
  sourcecode/telemetry/filters.py,sha256=Asa71oRl7q3Wt_FMwuufIZJFzSYdgRNKS8LHCIyFeYE,4805
62
63
  sourcecode/telemetry/transport.py,sha256=KJeIPCPWMdmbCP3ySGs2iUlia34U6vWne2dZsUezesw,1560
63
- sourcecode-1.5.0.dist-info/METADATA,sha256=kdZtCFuIhrWj6KDM4nZ-tMqWzuwiY2hXt4C8hP6PETc,20411
64
- sourcecode-1.5.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
65
- sourcecode-1.5.0.dist-info/entry_points.txt,sha256=ex3F9rmbXeyDIoFQHtkEqTsKSaJow8F0LrVu8XfIktQ,57
66
- sourcecode-1.5.0.dist-info/licenses/LICENSE,sha256=7DdHrU9Z_3e7dSvq4ISijZNjnuHo5NIHNiHDouMQ9JU,10491
67
- sourcecode-1.5.0.dist-info/RECORD,,
64
+ sourcecode-1.7.0.dist-info/METADATA,sha256=Vl1rL1-LLgo7afbfxCc0-l2GLTsAfFZzSrvl53DuzQQ,20411
65
+ sourcecode-1.7.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
66
+ sourcecode-1.7.0.dist-info/entry_points.txt,sha256=ex3F9rmbXeyDIoFQHtkEqTsKSaJow8F0LrVu8XfIktQ,57
67
+ sourcecode-1.7.0.dist-info/licenses/LICENSE,sha256=7DdHrU9Z_3e7dSvq4ISijZNjnuHo5NIHNiHDouMQ9JU,10491
68
+ sourcecode-1.7.0.dist-info/RECORD,,