Delete Demo.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576# Cell 11: Demo β Realistic Agent Behaviour (with failure, improvement, author negotiation)
# This demo shows the agent reasoning, trying a wrong fix, seeing tests fail, then improving.
# It uses the environmentβs tools and the authorβs feedback.
print("\n" + "="*70)
print("π¬ DEMO: Code Review Agent β Learning to Fix Bugs")
print("="*70)
env = CodeReviewEnv()
env.set_task("easy")
obs = env.reset()
print(f"\nπ TASK: {obs.pr_title}")
print(f"π DESCRIPTION: {obs.pr_description}")
print(f"\nπ» INITIAL CODE:\n{obs.code_snippet}\n")
# ------------------------------------------------------------------
# Step 1: Agent runs tests to see the failure
# ------------------------------------------------------------------
print("π§ STEP 1: Agent runs tests (run_tests)")
action = RunTests()
obs, reward, done, _ = env.step(action)
print(f"π¬ Test output:\n{obs.last_tool_output[:200]}...")
print(f"π Reward: {reward.value:.3f}\n")
# ------------------------------------------------------------------
# Step 2: Agent inspects the code and decides on a (wrong) fix
# ------------------------------------------------------------------
print("π§ STEP 2: Agent inspects the code and attempts a fix (WRONG)")
# Simulate a bad fix (e.g., adding a print instead of fixing null check)
bad_fix = "def get_user(id):\n print('debug')\n return users[id]"
action = ProposeFix(fix_code=bad_fix)
obs, reward, done, _ = env.step(action)
print(f"π¬ Proposed fix:\n{bad_fix}")
print(f"π¬ Test results:\n{obs.last_tool_output[:200]}...")
print(f"π Reward: {reward.value:.3f} (low β tests failed)\n")
# ------------------------------------------------------------------
# Step 3: Agent consults documentation to learn
# ------------------------------------------------------------------
print("π§ STEP 3: Agent queries documentation (query_docs: 'null check')")
action = QueryDocs(query_topic="null check")
obs, reward, done, _ = env.step(action)
print(f"π¬ Docs response:\n{obs.last_tool_output}")
print(f"π Reward: {reward.value:.3f}\n")
# ------------------------------------------------------------------
# Step 4: Agent writes a comment to negotiate with the author
# ------------------------------------------------------------------
print("π§ STEP 4: Agent comments to convince the author")
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.")
obs, reward, done, _ = env.step(action)
# Author's response is stored in env._comments
author_resp = [c for c in env._comments if "Author" in c][-1] if env._comments else ""
print(f"π¬ Agent comment delivered")
print(f"π¬ Author response: {author_resp}")
print(f"π Reward: {reward.value:.3f}\n")
# ------------------------------------------------------------------
# Step 5: Agent proposes the correct fix
# ------------------------------------------------------------------
print("π§ STEP 5: Agent proposes the correct fix")
good_fix = "def get_user(id):\n if id in users:\n return users[id]"
action = ProposeFix(fix_code=good_fix)
obs, reward, done, _ = env.step(action)
print(f"π¬ Proposed fix:\n{good_fix}")
print(f"π¬ Test results:\n{obs.last_tool_output[:300]}")
print(f"π Final Reward: {reward.value:.3f}\n")
# ------------------------------------------------------------------
# Step 6: Show final reward breakdown (optional)
# ------------------------------------------------------------------
print("="*70)
print("β
Demo complete. The agent learned from test failures, used documentation,")
print(" negotiated with the author, and finally fixed the bug correctly.")
print("="*70)Demo.pyDeleted-76