पाठ 5 / 29
Codebase पढ़ना और खोजना
Repo maps, grep-शैली खोज और agents चुनकर क्यों पढ़ते हैं, समझें।
पहले सही files खोजें
असली repository context window से कहीं बड़ा होता है, इसलिए agent को कोड बदलने से पहले प्रासंगिक कोड खोजना होता है। आम tools: file सूची और glob (नाम से), पाठ खोज (grep या ripgrep, symbols और strings के लिए), file पढ़ना (अक्सर tokens बचाने को पंक्ति-सीमा से), और repo map: files और उनकी classes व functions की संक्षिप्त रूपरेखा, कोड parse करके बनी, जो मॉडल को कुछ सौ tokens में project का आकार देखने और तय करने देती है कि क्या खोलना है। कुछ tools embeddings से semantic search, या "go to definition" और "find references" जैसी language-server सुविधाएँ जोड़ते हैं। अच्छे agents चुनकर और बार-बार पढ़ते हैं: खोजें, छोटी सीमा खोलें, संदर्भ का पीछा करें, फिर संपादित करें। बहुत कम पढ़ना ग़लत संपादन करवाता है; सब कुछ पढ़ना context और पैसा बर्बाद करता है।
देखें, संपादित करें, चलाएँ, दर्ज करें
विश्वसनीय tools का छोटा समूह — search, read, edit, run और git — अधिकांश काम करता है।
ast से repo map बनाना, चलाकर
मैंने यह सादे Python 3 (सिर्फ़ standard library) से चलाया, अस्थायी folder में बनाए throwaway project के साथ। कई उदाहरणों में प्रयुक्त छोटा नमूना project make_project सहायक से बनता है जो उसे उपयोग करने वाले पहले उदाहरण में दिखाया है; बाद के उदाहरण उसी सहायक से शुरू होते हैं। Script हर Python file parse करके classes, methods और functions छापता है: आठ पंक्तियाँ जो मॉडल को पूरे project का आकार दिखाती हैं। नमूना project में छोटा pricing module, cart और tests हैं।
import os, tempfile, textwrap
def make_project(root):
files = {
"shop/__init__.py": "",
"shop/pricing.py": textwrap.dedent("""
def apply_discount(price, percent):
\"\"\"Return price after a percentage discount.\"\"\"
return price - price * percent / 10
def add_tax(price, rate=0.18):
return round(price * (1 + rate), 2)
"""),
"shop/cart.py": textwrap.dedent("""
from shop.pricing import apply_discount, add_tax
class Cart:
def __init__(self):
self.items = []
def add(self, name, price):
self.items.append((name, price))
def total(self, discount_percent=0):
subtotal = sum(p for _, p in self.items)
return add_tax(apply_discount(subtotal, discount_percent))
"""),
"tests/__init__.py": "",
"tests/test_pricing.py": textwrap.dedent("""
import unittest
from shop.pricing import apply_discount, add_tax
class PricingTests(unittest.TestCase):
def test_discount_ten_percent(self):
self.assertEqual(apply_discount(200, 10), 180)
def test_discount_zero(self):
self.assertEqual(apply_discount(200, 0), 200)
def test_tax(self):
self.assertEqual(add_tax(100), 118.0)
"""),
}
for rel, text in files.items():
path = os.path.join(root, rel)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(text.lstrip("\n"))
import ast
def outline(root):
lines = []
for dirpath, _, names in sorted(os.walk(root)):
for name in sorted(names):
if not name.endswith(".py"): continue
rel = os.path.relpath(os.path.join(dirpath, name), root)
tree = ast.parse(open(os.path.join(dirpath, name)).read())
symbols = []
for node in tree.body:
if isinstance(node, ast.FunctionDef):
symbols.append(f"def {node.name}({', '.join(a.arg for a in node.args.args)})")
elif isinstance(node, ast.ClassDef):
methods = [n.name for n in node.body if isinstance(n, ast.FunctionDef)]
symbols.append(f"class {node.name}: {', '.join(methods)}")
if symbols:
lines.append(rel + "\n " + "\n ".join(symbols))
return "\n".join(lines)
with tempfile.TemporaryDirectory() as root:
make_project(root)
print(outline(root))
Output:
shop/cart.py class Cart: __init__, add, total shop/pricing.py def apply_discount(price, percent) def add_tax(price, rate) tests/test_pricing.py class PricingTests: test_discount_ten_percent, test_discount_zero, test_tax
त्वरित जाँच: Repo map किसलिए है?
- Repository encrypt करना
- Project का आकार संक्षेप में दिखाना ताकि मॉडल चुन सके क्या खोलना है
- Tests चलाना
- मृत कोड हटाना
Answer
Project का आकार संक्षेप में दिखाना ताकि मॉडल चुन सके क्या खोलना है — संक्षिप्त रूपरेखा पूरी context window ख़र्च किए बिना नेविगेशन में मदद करती है।