-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci_fix.py
More file actions
109 lines (83 loc) · 3.23 KB
/
Copy pathci_fix.py
File metadata and controls
109 lines (83 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
"""CI fix workflow: parse a CI failure log and use an agent to fix it.
Creates a buggy Python file and a test that catches the bug, then uses
CIFixWorkflow to diagnose the failure from a simulated CI log and fix it.
Usage:
export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="your-token"
export ANTHROPIC_MODEL="glm-5"
python examples/ci_fix.py
"""
from __future__ import annotations
import os
import sys
import tempfile
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import chimera
from chimera.ci import CIFixWorkflow
BUGGY_CODE = '''\
def add(a, b):
"""Add two numbers."""
return a - b # BUG: should be a + b
'''
TEST_CODE = '''\
from calculator import add
def test_add():
assert add(2, 3) == 5, "Expected 5"
assert add(0, 0) == 0, "Expected 0"
assert add(-1, 1) == 0, "Expected 0"
'''
# Simulated CI log output from running pytest
CI_LOG = """\
============================= test session starts ==============================
collected 1 item
test_calculator.py::test_add FAILED
=================================== FAILURES ===================================
__________________________________ test_add ____________________________________
def test_add():
> assert add(2, 3) == 5, "Expected 5"
E AssertionError: Expected 5
E assert -1 == 5
test_calculator.py:4: AssertionError
=========================== short test summary info ============================
FAILED test_calculator.py::test_add - AssertionError: Expected 5
=========================== 1 failed in 0.01s ==================================
"""
def main():
try:
provider = chimera.create_provider()
except ValueError as _e:
import sys
print(f"Setup error: {_e}", file=sys.stderr)
print("Set ANTHROPIC_API_KEY or ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN + ANTHROPIC_MODEL before running.", file=sys.stderr)
sys.exit(1)
with tempfile.TemporaryDirectory(prefix="chimera-cifix-") as tmpdir:
# Write the buggy code and test
with open(os.path.join(tmpdir, "calculator.py"), "w") as f:
f.write(BUGGY_CODE)
with open(os.path.join(tmpdir, "test_calculator.py"), "w") as f:
f.write(TEST_CODE)
env = chimera.LocalEnvironment(workdir=tmpdir)
env.setup()
agent = chimera.Agent(
provider=provider,
tools=list(chimera.AGENT_TOOLS),
loop=chimera.ReAct(max_steps=10),
)
print("=== CI Fix Workflow ===\n")
print(f"Workdir: {tmpdir}")
print("Bug: calculator.py uses subtraction instead of addition\n")
# Run the CI fix workflow
workflow = CIFixWorkflow(max_attempts=2)
fixed = workflow.run(CI_LOG, agent=agent, env=env)
print(f"Fixed: {fixed}")
print(f"Attempts: {len(workflow.attempts)}")
print(f"Total cost: ${workflow.total_cost:.4f}")
# Show the fixed file
fixed_path = os.path.join(tmpdir, "calculator.py")
if os.path.exists(fixed_path):
print("\n--- calculator.py (after fix) ---")
print(open(fixed_path).read())
env.cleanup()
if __name__ == "__main__":
main()