Return minimum context an agent needs to start any task (~100 tokens). Combines graph stats, top communities, top flows, risk score, and suggested next tools into an ultra-compact response. Args: task: Natural language description of what the agent is doing (e.g.
(
task: str = "",
changed_files: list[str] | None = None,
repo_root: str | None = None,
base: str = "HEAD~1",
)
| 35 | |
| 36 | |
| 37 | def get_minimal_context( |
| 38 | task: str = "", |
| 39 | changed_files: list[str] | None = None, |
| 40 | repo_root: str | None = None, |
| 41 | base: str = "HEAD~1", |
| 42 | ) -> dict[str, Any]: |
| 43 | """Return minimum context an agent needs to start any task (~100 tokens). |
| 44 | |
| 45 | Combines graph stats, top communities, top flows, risk score, |
| 46 | and suggested next tools into an ultra-compact response. |
| 47 | |
| 48 | Args: |
| 49 | task: Natural language description of what the agent is doing |
| 50 | (e.g. "review PR #42", "debug login timeout"). |
| 51 | changed_files: Explicit changed files. Auto-detected from git if None. |
| 52 | repo_root: Repository root path. Auto-detected if None. |
| 53 | base: Git ref for diff comparison. |
| 54 | """ |
| 55 | store, root = _get_store(repo_root) |
| 56 | try: |
| 57 | # 1. Quick stats |
| 58 | stats = store.get_stats() |
| 59 | |
| 60 | # 2. Risk from changed files |
| 61 | risk = "unknown" |
| 62 | risk_score = 0.0 |
| 63 | top_affected: list[str] = [] |
| 64 | test_gap_count = 0 |
| 65 | if changed_files or _has_git_changes(root, base): |
| 66 | try: |
| 67 | from ..changes import analyze_changes |
| 68 | from ..incremental import get_changed_files as _get_changed |
| 69 | |
| 70 | files = changed_files |
| 71 | if not files: |
| 72 | files = _get_changed(root, base) |
| 73 | if files: |
| 74 | abs_files = [str(root / f) for f in files] |
| 75 | analysis = analyze_changes( |
| 76 | store, abs_files, repo_root=str(root), base=base, |
| 77 | ) |
| 78 | risk_score = analysis.get("risk_score", 0.0) |
| 79 | risk = ( |
| 80 | "high" if risk_score > 0.7 |
| 81 | else "medium" if risk_score > 0.4 |
| 82 | else "low" |
| 83 | ) |
| 84 | top_affected = [ |
| 85 | f.get("name", "") |
| 86 | for f in analysis.get("changed_functions", [])[:5] |
| 87 | ] |
| 88 | test_gap_count = len(analysis.get("test_gaps", [])) |
| 89 | except ( |
| 90 | ImportError, OSError, ValueError, |
| 91 | sqlite3.Error, subprocess.SubprocessError, |
| 92 | ): |
| 93 | logger.debug("Risk analysis failed in get_minimal_context", exc_info=True) |
| 94 |