# Commands और Tests चलाना और परिणाम पढ़ना — Coding Agents और AI-Assisted Development

Source: https://www.geekswithgeeks.com/hi/coding-agents/t-run

> शोर वाले command आउटपुट को संक्षिप्त अवलोकन में बदलें।

## वह feedback जो agents को काम करवाता है

कोड चलाना ही agent को पाठ-जनरेटर से अलग करता है। Harness **shell/command tool** देता है (अक्सर **timeout**, आउटपुट सीमाओं और project पर निश्चित working directory के साथ) और आम तौर पर **tests, linters, type checkers और builds** के सुविधाजनक wrappers। कच्चा आउटपुट विशाल हो सकता है: अच्छा tool **संक्षिप्त अवलोकन** लौटाता है: exit code, कितने tests चले, **कौन-से विफल हुए**, और **पहला assertion संदेश व stack frame**, पूरा log माँगने पर उपलब्ध। Agent इसी से अगला चरण तय करता है। Commands नुक़सान भी कर सकती हैं (files हटाना, packages install करना, network calls), इसलिए कौन-सी commands स्वतः चलें, किन्हें अनुमोदन चाहिए और कौन-सी अस्वीकृत हों, यह **नीति** (guardrails कोर्स देखें) से तय हो, मॉडल के भरोसे नहीं।

## संक्षिप्त अवलोकन लौटाने वाला test-runner tool, चलाकर

मैंने यह सादे Python 3 (सिर्फ़ standard library) से चलाया, अस्थायी folder में बनाए throwaway project के साथ। नमूना project के तीन tests चलाने पर tool exit code 1, "3 tests", ठीक कौन-सा test विफल हुआ (`test_discount_ten_percent`) और assertion संदेश `0.0 != 180` बताता है। मॉडल के लिए पन्नों का log पढ़े बिना bug खोजने को यह काफ़ी है।

```python
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 subprocess, sys, re

def run_tests(root):
    p = subprocess.run([sys.executable, "-m", "unittest", "discover", "-s", "tests", "-t", "."],
                       cwd=root, capture_output=True, text=True)
    out = p.stderr
    summary = re.search(r"Ran (\d+) tests? in", out).group(1) + " tests"
    failed = re.findall(r"^(?:FAIL|ERROR): (\S+)", out, re.M)
    first = re.search(r"AssertionError: .*", out)
    return {"exit_code": p.returncode, "summary": summary, "failed": failed, "first_error": first.group(0) if first else None}

with tempfile.TemporaryDirectory() as root:
    make_project(root)
    print(run_tests(root))

```

Output:

```
{'exit_code': 1, 'summary': '3 tests', 'failed': ['test_discount_ten_percent'], 'first_error': 'AssertionError: 0.0 != 180'}
```

## हमेशा timeout रखें

अटका test या interactive prompt agent को हमेशा के लिए जमा सकता है। हर command को समय सीमा और non-interactive mode दें।

**Quiz:** Test tool को मॉडल को क्या लौटाना चाहिए?

- [x] संक्षिप्त सार: exit code, विफल tests और पहली त्रुटि
- [ ] कुछ भी नहीं
- [ ] सिर्फ़ शब्द "done"
- [ ] हर बार पूरा log

*Answer:* संक्षिप्त सार: exit code, विफल tests और पहली त्रुटि. संक्षिप्त, प्रासंगिक अवलोकन context बचाते और सुधार तेज़ करते हैं।
