|
| 1 | +# @WHO: scripts/agent_guide.py |
| 2 | +# @WHAT: Task-oriented usage guide for agents (`codelens --guide`) |
| 3 | +# @PART: cli |
| 4 | +# @ENTRY: build_guide() |
| 5 | +"""The guide a new agent reads to use CodeLens without guessing. |
| 6 | +
|
| 7 | +`codelens --help` lists commands; this answers "I want to do X — what do I |
| 8 | +run?" with copy-paste examples, spells out the positional conventions, and |
| 9 | +bakes in the traps found by dogfooding (search's pattern positional, trace's |
| 10 | +--domain backend). Sub-checks are derived live from each umbrella's `_CHECKS` |
| 11 | +so the guide can't drift from the code. |
| 12 | +""" |
| 13 | + |
| 14 | +import importlib |
| 15 | +import json |
| 16 | +from typing import Any, Dict, List |
| 17 | + |
| 18 | + |
| 19 | +# Task → command. Intent can't be derived from the registry, so this is |
| 20 | +# curated — but every command here is a real, tested invocation. `<ws>` is the |
| 21 | +# workspace path (auto-detected if omitted); `FN` a function name. |
| 22 | +_TASKS: List[Dict[str, str]] = [ |
| 23 | + {"task": "Orient in an unfamiliar codebase", |
| 24 | + "run": "codelens context <ws>"}, |
| 25 | + {"task": "Who calls this function (callers)", |
| 26 | + "run": "codelens context <ws> --check trace --name FN --direction up --domain backend"}, |
| 27 | + {"task": "What does this function call (callees)", |
| 28 | + "run": "codelens context <ws> --check trace --name FN --direction down --domain backend"}, |
| 29 | + {"task": "Read one function's source (skip reading the whole file)", |
| 30 | + "run": "codelens context <ws> --check source --name FN"}, |
| 31 | + {"task": "Blast radius before changing a symbol", |
| 32 | + "run": "codelens impact <ws> --name FN"}, |
| 33 | + {"task": "A file's structure (functions/classes)", |
| 34 | + "run": "codelens context <ws> --check outline --file path/to/file.py"}, |
| 35 | + {"task": "Find a symbol by name", |
| 36 | + "run": "codelens search FN --mode symbol", |
| 37 | + "note": "PATTERN is the first positional (workspace optional second): " |
| 38 | + "`codelens search FN [<ws>] --mode symbol` — the reverse of every " |
| 39 | + "other command."}, |
| 40 | + {"task": "Regex / full-text search", |
| 41 | + "run": "codelens search 'PATTERN' --mode regex"}, |
| 42 | + {"task": "Find dead code", |
| 43 | + "run": "codelens audit <ws> --check dead-code"}, |
| 44 | + {"task": "Collect a named @FLOW's scattered functions", |
| 45 | + "run": "codelens context <ws> --check flow --name FLOW_NAME"}, |
| 46 | + {"task": "Did a named flow's shape change between two snapshots", |
| 47 | + "run": "codelens impact <ws> --check flow-diff --name FLOW_NAME"}, |
| 48 | + {"task": "Audit @FLOW/@ENTRY doc-tags & coverage", |
| 49 | + "run": "codelens context <ws> --check tags"}, |
| 50 | + {"task": "Circular dependencies", |
| 51 | + "run": "codelens deps <ws> --check circular"}, |
| 52 | + {"task": "Scan / (re)build the graph for a workspace", |
| 53 | + "run": "codelens scan <ws>"}, |
| 54 | + {"task": "Scan for secrets", |
| 55 | + "run": "codelens security <ws> --check secrets"}, |
| 56 | +] |
| 57 | + |
| 58 | +_CONVENTIONS = [ |
| 59 | + "Most commands take the workspace as the first positional: " |
| 60 | + "`codelens <command> <workspace> [--check X] [--name Y]`.", |
| 61 | + "EXCEPTION — `search` is `codelens search <pattern> [<workspace>] " |
| 62 | + "--mode symbol|regex|semantic|graph`: the PATTERN is the first positional, " |
| 63 | + "the workspace an optional second (not the other way round).", |
| 64 | + "Add `--format json` (structured) or `--format compact` (token-lean) for " |
| 65 | + "machine parsing; the default is human-readable.", |
| 66 | + "`trace` and `context --check context` resolve backend symbols best with " |
| 67 | + "`--domain backend` (the default `auto` can miss them).", |
| 68 | + "On a CLI argument error with a machine format, the error is printed to " |
| 69 | + "stdout as `{\"s\":\"error\",...}` — so an empty result is a real empty, " |
| 70 | + "not a hidden error.", |
| 71 | + "Run `scan` once so graph-backed checks (impact, trace, flow subgraph, " |
| 72 | + "source-by-name) have data; they degrade gracefully without it.", |
| 73 | +] |
| 74 | + |
| 75 | +# Umbrellas whose sub-checks come from a `_CHECKS` dict, plus the odd ones out. |
| 76 | +_UMBRELLAS = ["context", "impact", "audit", "deps", "security", "api_map"] |
| 77 | +_NON_CHECK = { |
| 78 | + "search": "modes: semantic (default) | symbol | regex | graph " |
| 79 | + "(via --mode; pattern is positional)", |
| 80 | + "summary": "workspace summary (no --check)", |
| 81 | + "scan": "build/refresh the registry (no --check)", |
| 82 | + "doctor": "environment audit (no --check)", |
| 83 | + "history": "historical trend data (no --check)", |
| 84 | + "graph": "raw Cypher-subset query (power-user)", |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +def _live_checks() -> Dict[str, List[str]]: |
| 89 | + """Sub-checks per umbrella, read straight from the code.""" |
| 90 | + out: Dict[str, List[str]] = {} |
| 91 | + for name in _UMBRELLAS: |
| 92 | + try: |
| 93 | + mod = importlib.import_module(f"commands.{name}") |
| 94 | + checks = getattr(mod, "_CHECKS", None) |
| 95 | + if checks: |
| 96 | + out[name.replace("_", "-")] = list(checks.keys()) |
| 97 | + except Exception: |
| 98 | + continue |
| 99 | + return out |
| 100 | + |
| 101 | + |
| 102 | +def build_guide(fmt: str = "text") -> Any: |
| 103 | + """The agent usage guide. Returns a dict for machine formats, else text.""" |
| 104 | + checks = _live_checks() |
| 105 | + guide = { |
| 106 | + "tool": "codelens", |
| 107 | + "how_to_read": "Find your task below and run the command; replace " |
| 108 | + "<ws>/FN/FLOW_NAME with your values.", |
| 109 | + "conventions": _CONVENTIONS, |
| 110 | + "tasks": _TASKS, |
| 111 | + "commands": { |
| 112 | + **{u: {"checks": c} for u, c in checks.items()}, |
| 113 | + **{u: {"note": n} for u, n in _NON_CHECK.items()}, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + if fmt in ("json", "compact", "ai", "sarif", "graphml", "junit-xml", "gitlab-sast"): |
| 118 | + return guide |
| 119 | + |
| 120 | + return _render_text(guide) |
| 121 | + |
| 122 | + |
| 123 | +def _render_text(guide: Dict) -> str: |
| 124 | + lines = ["# CodeLens — agent guide", "", guide["how_to_read"], "", |
| 125 | + "## Conventions"] |
| 126 | + for c in guide["conventions"]: |
| 127 | + lines.append(f"- {c}") |
| 128 | + lines += ["", "## Tasks → commands"] |
| 129 | + for t in guide["tasks"]: |
| 130 | + lines.append(f"- {t['task']}:") |
| 131 | + lines.append(f" {t['run']}") |
| 132 | + if t.get("note"): |
| 133 | + lines.append(f" note: {t['note']}") |
| 134 | + lines += ["", "## Commands & sub-checks"] |
| 135 | + for cmd, info in guide["commands"].items(): |
| 136 | + if "checks" in info: |
| 137 | + lines.append(f"- {cmd}: --check " + " | ".join(info["checks"])) |
| 138 | + else: |
| 139 | + lines.append(f"- {cmd}: {info['note']}") |
| 140 | + return "\n".join(lines) |
0 commit comments