Delete Demo.py
Browse files
Demo.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
# Cell 11: Demo β Realistic Agent Behaviour (with failure, improvement, author negotiation)
|
| 2 |
-
# This demo shows the agent reasoning, trying a wrong fix, seeing tests fail, then improving.
|
| 3 |
-
# It uses the environmentβs tools and the authorβs feedback.
|
| 4 |
-
|
| 5 |
-
print("\n" + "="*70)
|
| 6 |
-
print("π¬ DEMO: Code Review Agent β Learning to Fix Bugs")
|
| 7 |
-
print("="*70)
|
| 8 |
-
|
| 9 |
-
env = CodeReviewEnv()
|
| 10 |
-
env.set_task("easy")
|
| 11 |
-
obs = env.reset()
|
| 12 |
-
|
| 13 |
-
print(f"\nπ TASK: {obs.pr_title}")
|
| 14 |
-
print(f"π DESCRIPTION: {obs.pr_description}")
|
| 15 |
-
print(f"\nπ» INITIAL CODE:\n{obs.code_snippet}\n")
|
| 16 |
-
|
| 17 |
-
# ------------------------------------------------------------------
|
| 18 |
-
# Step 1: Agent runs tests to see the failure
|
| 19 |
-
# ------------------------------------------------------------------
|
| 20 |
-
print("π§ STEP 1: Agent runs tests (run_tests)")
|
| 21 |
-
action = RunTests()
|
| 22 |
-
obs, reward, done, _ = env.step(action)
|
| 23 |
-
print(f"π¬ Test output:\n{obs.last_tool_output[:200]}...")
|
| 24 |
-
print(f"π Reward: {reward.value:.3f}\n")
|
| 25 |
-
|
| 26 |
-
# ------------------------------------------------------------------
|
| 27 |
-
# Step 2: Agent inspects the code and decides on a (wrong) fix
|
| 28 |
-
# ------------------------------------------------------------------
|
| 29 |
-
print("π§ STEP 2: Agent inspects the code and attempts a fix (WRONG)")
|
| 30 |
-
# Simulate a bad fix (e.g., adding a print instead of fixing null check)
|
| 31 |
-
bad_fix = "def get_user(id):\n print('debug')\n return users[id]"
|
| 32 |
-
action = ProposeFix(fix_code=bad_fix)
|
| 33 |
-
obs, reward, done, _ = env.step(action)
|
| 34 |
-
print(f"π¬ Proposed fix:\n{bad_fix}")
|
| 35 |
-
print(f"π¬ Test results:\n{obs.last_tool_output[:200]}...")
|
| 36 |
-
print(f"π Reward: {reward.value:.3f} (low β tests failed)\n")
|
| 37 |
-
|
| 38 |
-
# ------------------------------------------------------------------
|
| 39 |
-
# Step 3: Agent consults documentation to learn
|
| 40 |
-
# ------------------------------------------------------------------
|
| 41 |
-
print("π§ STEP 3: Agent queries documentation (query_docs: 'null check')")
|
| 42 |
-
action = QueryDocs(query_topic="null check")
|
| 43 |
-
obs, reward, done, _ = env.step(action)
|
| 44 |
-
print(f"π¬ Docs response:\n{obs.last_tool_output}")
|
| 45 |
-
print(f"π Reward: {reward.value:.3f}\n")
|
| 46 |
-
|
| 47 |
-
# ------------------------------------------------------------------
|
| 48 |
-
# Step 4: Agent writes a comment to negotiate with the author
|
| 49 |
-
# ------------------------------------------------------------------
|
| 50 |
-
print("π§ STEP 4: Agent comments to convince the author")
|
| 51 |
-
action = WriteComment(comment_text="I checked the docs β a missing key will raise KeyError. We should add a condition: if id in users: return users[id] else: handle missing key.")
|
| 52 |
-
obs, reward, done, _ = env.step(action)
|
| 53 |
-
# Author's response is stored in env._comments
|
| 54 |
-
author_resp = [c for c in env._comments if "Author" in c][-1] if env._comments else ""
|
| 55 |
-
print(f"π¬ Agent comment delivered")
|
| 56 |
-
print(f"π¬ Author response: {author_resp}")
|
| 57 |
-
print(f"π Reward: {reward.value:.3f}\n")
|
| 58 |
-
|
| 59 |
-
# ------------------------------------------------------------------
|
| 60 |
-
# Step 5: Agent proposes the correct fix
|
| 61 |
-
# ------------------------------------------------------------------
|
| 62 |
-
print("π§ STEP 5: Agent proposes the correct fix")
|
| 63 |
-
good_fix = "def get_user(id):\n if id in users:\n return users[id]"
|
| 64 |
-
action = ProposeFix(fix_code=good_fix)
|
| 65 |
-
obs, reward, done, _ = env.step(action)
|
| 66 |
-
print(f"π¬ Proposed fix:\n{good_fix}")
|
| 67 |
-
print(f"π¬ Test results:\n{obs.last_tool_output[:300]}")
|
| 68 |
-
print(f"π Final Reward: {reward.value:.3f}\n")
|
| 69 |
-
|
| 70 |
-
# ------------------------------------------------------------------
|
| 71 |
-
# Step 6: Show final reward breakdown (optional)
|
| 72 |
-
# ------------------------------------------------------------------
|
| 73 |
-
print("="*70)
|
| 74 |
-
print("β
Demo complete. The agent learned from test failures, used documentation,")
|
| 75 |
-
print(" negotiated with the author, and finally fixed the bug correctly.")
|
| 76 |
-
print("="*70)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|