Compare commits
2 Commits
c476b1a37a
...
325c0767f1
Author | SHA1 | Date | |
---|---|---|---|
325c0767f1 | |||
0b9902c428 |
|
@ -134,13 +134,13 @@ def bash_cmd(*commands: str) -> str:
|
|||
|
||||
|
||||
AIDER_TEST = bash_cmd(
|
||||
'echo "Setting up virtual environment"'
|
||||
'echo "Setting up virtual environment"',
|
||||
'virtualenv venv',
|
||||
'echo "Activating virtual environment"'
|
||||
'echo "Activating virtual environment"',
|
||||
'source venv/bin/activate',
|
||||
'echo "Installing package"'
|
||||
'echo "Installing package"',
|
||||
'pip install -e .',
|
||||
'echo "Testing package"'
|
||||
'echo "Testing package"',
|
||||
'pytest test',
|
||||
)
|
||||
|
||||
|
@ -165,6 +165,18 @@ Go ahead with the changes you deem appropriate without waiting for explicit appr
|
|||
Do not draft changes beforehand; produce changes only once prompted for a specific file.
|
||||
"""
|
||||
|
||||
CLAUDE_CODE_MESSAGE_FORMAT = """{issue}
|
||||
|
||||
Please fix this issue by making the necessary code changes. Follow these guidelines:
|
||||
1. Run tests after making changes to ensure they pass
|
||||
2. Follow existing code style and conventions
|
||||
3. Make minimal, focused changes to address the issue
|
||||
4. Commit your changes with a descriptive message
|
||||
|
||||
The test command for this project is: {test_command}
|
||||
The lint command for this project is: {lint_command}
|
||||
"""
|
||||
|
||||
CODE_MODEL = None
|
||||
EVALUATOR_MODEL = 'ollama/gemma3:27b'
|
||||
|
||||
|
@ -174,14 +186,52 @@ MODEL_EDIT_MODES = {
|
|||
}
|
||||
|
||||
|
||||
def create_aider_command(issue: str) -> list[str]:
|
||||
def run_post_solver_cleanup(repository_path: Path, solver_name: str) -> None:
|
||||
"""Run standard code quality fixes and commit changes after a code solver.
|
||||
|
||||
Args:
|
||||
repository_path: Path to the repository
|
||||
solver_name: Name of the solver (for commit message)
|
||||
"""
|
||||
# Auto-fix standard code quality stuff
|
||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], repository_path, check=False)
|
||||
run_cmd(['git', 'add', '.'], repository_path)
|
||||
run_cmd(
|
||||
['git', 'commit', '-m', f'Ruff after {solver_name}'],
|
||||
repository_path,
|
||||
check=False,
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class CodeSolverStrategy:
|
||||
"""Base interface for code solving strategies."""
|
||||
|
||||
def solve_issue_round(self, repository_path: Path, issue_content: str) -> bool:
|
||||
"""Attempt to solve an issue in a single round.
|
||||
|
||||
Args:
|
||||
repository_path: Path to the repository
|
||||
issue_content: The issue description to solve
|
||||
|
||||
Returns:
|
||||
True if the solution round completed without crashing, False otherwise
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class AiderCodeSolver(CodeSolverStrategy):
|
||||
"""Code solver that uses Aider for issue resolution."""
|
||||
|
||||
def _create_aider_command(self, issue: str) -> list[str]:
|
||||
"""Create the Aider command with all necessary flags."""
|
||||
l = [
|
||||
'aider',
|
||||
'--chat-language',
|
||||
'english',
|
||||
'--no-stream',
|
||||
'--no-analytics',
|
||||
#'--no-check-update',
|
||||
'--test-cmd',
|
||||
AIDER_TEST,
|
||||
'--lint-cmd',
|
||||
|
@ -225,6 +275,120 @@ def create_aider_command(issue: str) -> list[str]:
|
|||
|
||||
return l
|
||||
|
||||
def solve_issue_round(self, repository_path: Path, issue_content: str) -> bool:
|
||||
"""Solve an issue using Aider."""
|
||||
# Primary Aider command
|
||||
aider_command = self._create_aider_command(issue_content)
|
||||
aider_did_not_crash = run_cmd(
|
||||
aider_command,
|
||||
repository_path,
|
||||
check=False,
|
||||
)
|
||||
if not aider_did_not_crash:
|
||||
return aider_did_not_crash
|
||||
|
||||
# Run post-solver cleanup
|
||||
run_post_solver_cleanup(repository_path, 'aider')
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class ClaudeCodeSolver(CodeSolverStrategy):
|
||||
"""Code solver that uses Claude Code for issue resolution."""
|
||||
|
||||
def _create_claude_command(self, issue: str) -> list[str]:
|
||||
"""Create the Claude Code command for programmatic use."""
|
||||
cmd = [
|
||||
'claude',
|
||||
'-p',
|
||||
'--output-format',
|
||||
'json',
|
||||
'--max-turns',
|
||||
'10',
|
||||
]
|
||||
|
||||
if CODE_MODEL:
|
||||
cmd.extend(['--model', CODE_MODEL])
|
||||
|
||||
cmd.append(issue)
|
||||
return cmd
|
||||
|
||||
def solve_issue_round(self, repository_path: Path, issue_content: str) -> bool:
|
||||
"""Solve an issue using Claude Code."""
|
||||
import json
|
||||
import os
|
||||
|
||||
# Set Anthropic API key environment variable
|
||||
env = os.environ.copy()
|
||||
env['ANTHROPIC_API_KEY'] = secrets.anthropic_api_key()
|
||||
|
||||
# Prepare the issue prompt for Claude Code
|
||||
enhanced_issue = CLAUDE_CODE_MESSAGE_FORMAT.format(
|
||||
issue=issue_content,
|
||||
test_command=AIDER_TEST,
|
||||
lint_command=AIDER_LINT,
|
||||
)
|
||||
|
||||
# Create Claude Code command
|
||||
claude_command = self._create_claude_command(enhanced_issue)
|
||||
|
||||
# Run Claude Code
|
||||
result = subprocess.run(
|
||||
claude_command,
|
||||
cwd=repository_path,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error('Claude Code failed with return code %d', result.returncode)
|
||||
logger.error('stderr: %s', result.stderr)
|
||||
return False
|
||||
|
||||
# Parse response if it's JSON
|
||||
try:
|
||||
if result.stdout.strip():
|
||||
response_data = json.loads(result.stdout)
|
||||
logger.info(
|
||||
'Claude Code response: %s',
|
||||
response_data.get('text', 'No text field'),
|
||||
)
|
||||
except json.JSONDecodeError:
|
||||
logger.info('Claude Code response (non-JSON): %s', result.stdout[:500])
|
||||
|
||||
# Run post-solver cleanup
|
||||
run_post_solver_cleanup(repository_path, 'Claude Code')
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def is_anthropic_model(model: str) -> bool:
|
||||
"""Check if the model string indicates an Anthropic/Claude model."""
|
||||
if not model:
|
||||
return False
|
||||
|
||||
anthropic_indicators = [
|
||||
'claude',
|
||||
'anthropic',
|
||||
'sonnet',
|
||||
'haiku',
|
||||
'opus',
|
||||
]
|
||||
|
||||
model_lower = model.lower()
|
||||
return any(indicator in model_lower for indicator in anthropic_indicators)
|
||||
|
||||
|
||||
def create_code_solver() -> CodeSolverStrategy:
|
||||
"""Create the appropriate code solver based on the configured model."""
|
||||
if is_anthropic_model(CODE_MODEL):
|
||||
return ClaudeCodeSolver()
|
||||
else:
|
||||
return AiderCodeSolver()
|
||||
|
||||
|
||||
def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> list[str]:
|
||||
"""Get commit messages between base branch and current branch.
|
||||
|
@ -345,33 +509,17 @@ def run_cmd(cmd: list[str], cwd: Path | None = None, check=True) -> bool:
|
|||
return result.returncode == 0
|
||||
|
||||
|
||||
def issue_solution_round(repository_path, issue_content):
|
||||
# Primary Aider command
|
||||
aider_command = create_aider_command(issue_content)
|
||||
aider_did_not_crash = run_cmd(
|
||||
aider_command,
|
||||
repository_path,
|
||||
check=False,
|
||||
)
|
||||
if not aider_did_not_crash:
|
||||
return aider_did_not_crash
|
||||
|
||||
# Auto-fix standard code quality stuff after aider
|
||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], repository_path, check=False)
|
||||
run_cmd(['git', 'add', '.'], repository_path)
|
||||
run_cmd(['git', 'commit', '-m', 'Ruff after aider'], repository_path, check=False)
|
||||
|
||||
return True
|
||||
|
||||
def remove_thinking_tokens(text: str) -> str:
|
||||
text = re.sub(r'^\s*<think>.*?</think>', '', text, flags=re.MULTILINE | re.DOTALL)
|
||||
text = text.strip()
|
||||
return text
|
||||
|
||||
|
||||
assert remove_thinking_tokens('<think>Hello</think>\nWorld\n') == 'World'
|
||||
assert remove_thinking_tokens('<think>\nHello\n</think>\nWorld\n') == 'World'
|
||||
assert remove_thinking_tokens('\n<think>\nHello\n</think>\nWorld\n') == 'World'
|
||||
|
||||
|
||||
def run_ollama(cwd: Path, texts: list[str]) -> str:
|
||||
cmd = ['ollama', 'run', EVALUATOR_MODEL.removeprefix('ollama/')]
|
||||
process = subprocess.Popen(
|
||||
|
@ -397,9 +545,11 @@ def parse_yes_no_answer(text: str) -> bool | None:
|
|||
return False
|
||||
return None
|
||||
|
||||
|
||||
assert parse_yes_no_answer('Yes.') == True
|
||||
assert parse_yes_no_answer('no') == False
|
||||
|
||||
|
||||
def run_ollama_and_get_yes_or_no(cwd, initial_texts: list[str]) -> bool:
|
||||
texts = list(initial_texts)
|
||||
texts.append('Think through your answer.')
|
||||
|
@ -455,6 +605,7 @@ def solve_issue_in_repository(
|
|||
issue_description: str,
|
||||
issue_number: str,
|
||||
gitea_client,
|
||||
code_solver: CodeSolverStrategy,
|
||||
) -> IssueResolution:
|
||||
logger.info('### %s #####', issue_title)
|
||||
|
||||
|
@ -464,28 +615,31 @@ def solve_issue_in_repository(
|
|||
run_cmd(['git', 'checkout', repository_config.base_branch], repository_path)
|
||||
run_cmd(['git', 'checkout', '-b', branch_name], repository_path)
|
||||
|
||||
# Run initial ruff pass before aider
|
||||
# Run initial ruff pass before code solver
|
||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], repository_path, check=False)
|
||||
run_cmd(['git', 'add', '.'], repository_path)
|
||||
run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], repository_path, check=False)
|
||||
|
||||
# Run aider
|
||||
# Run code solver
|
||||
issue_content = f'# {issue_title}\n{issue_description}'
|
||||
|
||||
while True:
|
||||
# Save the commit hash after ruff but before aider
|
||||
# Save the commit hash after ruff but before code solver
|
||||
pre_aider_commit = get_head_commit_hash(repository_path)
|
||||
|
||||
# Run aider
|
||||
aider_did_not_crash = issue_solution_round(repository_path, issue_content)
|
||||
if not aider_did_not_crash:
|
||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||
# Run code solver
|
||||
solver_did_not_crash = code_solver.solve_issue_round(
|
||||
repository_path,
|
||||
issue_content,
|
||||
)
|
||||
if not solver_did_not_crash:
|
||||
logger.error('Code solver invocation failed for issue #%s', issue_number)
|
||||
return IssueResolution(False)
|
||||
|
||||
# Check if aider made any changes beyond the initial ruff pass
|
||||
# Check if solver made any changes beyond the initial ruff pass
|
||||
if not has_commits_on_branch(repository_path, pre_aider_commit, 'HEAD'):
|
||||
logger.error(
|
||||
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||
'Code solver did not make any changes beyond the initial ruff pass for issue #%s',
|
||||
issue_number,
|
||||
)
|
||||
return IssueResolution(False)
|
||||
|
@ -538,6 +692,7 @@ def solve_issues_in_repository(
|
|||
logger.info('Skipping already processed issue #%s: %s', issue_number, title)
|
||||
else:
|
||||
branch_name = generate_branch_name(issue_number, title)
|
||||
code_solver = create_code_solver()
|
||||
with tempfile.TemporaryDirectory() as repository_path:
|
||||
issue_resolution = solve_issue_in_repository(
|
||||
repository_config,
|
||||
|
@ -547,6 +702,7 @@ def solve_issues_in_repository(
|
|||
issue_description,
|
||||
issue_number,
|
||||
client,
|
||||
code_solver,
|
||||
)
|
||||
seen_issues_db.mark_as_seen(issue_url, str(issue_number))
|
||||
seen_issues_db.update_pr_info(
|
||||
|
@ -571,6 +727,7 @@ def solve_issues_in_repository(
|
|||
client,
|
||||
seen_issues_db,
|
||||
issue_url,
|
||||
code_solver,
|
||||
)
|
||||
|
||||
# Handle failing pipelines
|
||||
|
@ -580,6 +737,7 @@ def solve_issues_in_repository(
|
|||
branch_name,
|
||||
Path(repository_path),
|
||||
client,
|
||||
code_solver,
|
||||
)
|
||||
|
||||
|
||||
|
@ -591,8 +749,9 @@ def handle_pr_comments(
|
|||
client,
|
||||
seen_issues_db,
|
||||
issue_url,
|
||||
code_solver: CodeSolverStrategy,
|
||||
):
|
||||
"""Fetch unresolved PR comments and resolve them via aider."""
|
||||
"""Fetch unresolved PR comments and resolve them via code solver."""
|
||||
comments = client.get_pull_request_comments(
|
||||
repository_config.owner,
|
||||
repository_config.repo,
|
||||
|
@ -614,8 +773,8 @@ def handle_pr_comments(
|
|||
f'Resolve the following reviewer comment:\n{body}\n\n'
|
||||
f'File: {path}\n\nContext:\n{context}'
|
||||
)
|
||||
# invoke aider on the comment context
|
||||
issue_solution_round(repository_path, issue)
|
||||
# invoke code solver on the comment context
|
||||
code_solver.solve_issue_round(repository_path, issue)
|
||||
# commit and push changes for this comment
|
||||
run_cmd(['git', 'add', path], repository_path, check=False)
|
||||
run_cmd(
|
||||
|
@ -632,8 +791,9 @@ def handle_failing_pipelines(
|
|||
branch_name: str,
|
||||
repository_path: Path,
|
||||
client,
|
||||
code_solver: CodeSolverStrategy,
|
||||
) -> None:
|
||||
"""Fetch failing pipelines for the given PR and resolve them via aider."""
|
||||
"""Fetch failing pipelines for the given PR and resolve them via code solver."""
|
||||
while True:
|
||||
failed_runs = client.get_failed_pipelines(
|
||||
repository_config.owner,
|
||||
|
@ -651,7 +811,7 @@ def handle_failing_pipelines(
|
|||
lines = log.strip().split('\n')
|
||||
context = '\n'.join(lines[-100:])
|
||||
issue = f'Resolve the following failing pipeline run {run_id}:\n\n{context}'
|
||||
issue_solution_round(repository_path, issue)
|
||||
code_solver.solve_issue_round(repository_path, issue)
|
||||
run_cmd(['git', 'add', '.'], repository_path, check=False)
|
||||
run_cmd(
|
||||
['git', 'commit', '-m', f'Resolve pipeline {run_id}'],
|
||||
|
|
|
@ -9,3 +9,7 @@ def llm_api_keys() -> list[str]:
|
|||
|
||||
def gitea_token() -> str:
|
||||
return SECRETS.load_or_fail('GITEA_TOKEN')
|
||||
|
||||
|
||||
def anthropic_api_key() -> str:
|
||||
return SECRETS.load_or_fail('ANTHROPIC_API_KEY')
|
||||
|
|
1
setup.py
1
setup.py
|
@ -109,6 +109,7 @@ def find_python_packages() -> list[str]:
|
|||
print(f'Found following packages: {packages}')
|
||||
return sorted(packages)
|
||||
|
||||
|
||||
with open(PACKAGE_NAME + '/_version.py') as f:
|
||||
version = parse_version_file(f.read())
|
||||
|
||||
|
|
122
test/test_claude_code_integration.py
Normal file
122
test/test_claude_code_integration.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
import pytest
|
||||
|
||||
from aider_gitea import (
|
||||
AIDER_LINT,
|
||||
AIDER_TEST,
|
||||
CLAUDE_CODE_MESSAGE_FORMAT,
|
||||
AiderCodeSolver,
|
||||
ClaudeCodeSolver,
|
||||
create_code_solver,
|
||||
is_anthropic_model,
|
||||
)
|
||||
|
||||
|
||||
class TestClaudeCodeIntegration:
|
||||
"""Test Claude Code integration and model routing logic."""
|
||||
|
||||
def test_is_anthropic_model_detection(self):
|
||||
"""Test that Anthropic models are correctly detected."""
|
||||
# Anthropic models should return True
|
||||
assert is_anthropic_model('claude-3-sonnet')
|
||||
assert is_anthropic_model('claude-3-haiku')
|
||||
assert is_anthropic_model('claude-3-opus')
|
||||
assert is_anthropic_model('anthropic/claude-3-sonnet')
|
||||
assert is_anthropic_model('Claude-3-Sonnet') # Case insensitive
|
||||
assert is_anthropic_model('ANTHROPIC/CLAUDE')
|
||||
assert is_anthropic_model('some-sonnet-model')
|
||||
assert is_anthropic_model('haiku-variant')
|
||||
|
||||
# Non-Anthropic models should return False
|
||||
assert not is_anthropic_model('gpt-4')
|
||||
assert not is_anthropic_model('gpt-3.5-turbo')
|
||||
assert not is_anthropic_model('ollama/llama')
|
||||
assert not is_anthropic_model('gemini-pro')
|
||||
assert not is_anthropic_model('mistral-7b')
|
||||
assert not is_anthropic_model('')
|
||||
assert not is_anthropic_model(None)
|
||||
|
||||
def test_create_code_solver_routing(self, monkeypatch):
|
||||
"""Test that the correct solver is created based on model."""
|
||||
import aider_gitea
|
||||
|
||||
# Test Anthropic model routing
|
||||
monkeypatch.setattr(aider_gitea, 'CODE_MODEL', 'claude-3-sonnet')
|
||||
solver = create_code_solver()
|
||||
assert isinstance(solver, ClaudeCodeSolver)
|
||||
|
||||
# Test non-Anthropic model routing
|
||||
monkeypatch.setattr(aider_gitea, 'CODE_MODEL', 'gpt-4')
|
||||
solver = create_code_solver()
|
||||
assert isinstance(solver, AiderCodeSolver)
|
||||
|
||||
# Test None model routing (should default to Aider)
|
||||
monkeypatch.setattr(aider_gitea, 'CODE_MODEL', None)
|
||||
solver = create_code_solver()
|
||||
assert isinstance(solver, AiderCodeSolver)
|
||||
|
||||
def test_claude_code_solver_command_creation(self):
|
||||
"""Test that Claude Code commands are created correctly."""
|
||||
import aider_gitea
|
||||
|
||||
solver = ClaudeCodeSolver()
|
||||
issue = 'Fix the bug in the code'
|
||||
|
||||
# Test without model
|
||||
with pytest.MonkeyPatch().context() as m:
|
||||
m.setattr(aider_gitea, 'CODE_MODEL', None)
|
||||
cmd = solver._create_claude_command(issue)
|
||||
expected = [
|
||||
'claude',
|
||||
'-p',
|
||||
'--output-format',
|
||||
'json',
|
||||
'--max-turns',
|
||||
'10',
|
||||
issue,
|
||||
]
|
||||
assert cmd == expected
|
||||
|
||||
# Test with model
|
||||
with pytest.MonkeyPatch().context() as m:
|
||||
m.setattr(aider_gitea, 'CODE_MODEL', 'claude-3-sonnet')
|
||||
cmd = solver._create_claude_command(issue)
|
||||
expected = [
|
||||
'claude',
|
||||
'-p',
|
||||
'--output-format',
|
||||
'json',
|
||||
'--max-turns',
|
||||
'10',
|
||||
'--model',
|
||||
'claude-3-sonnet',
|
||||
issue,
|
||||
]
|
||||
assert cmd == expected
|
||||
|
||||
def test_claude_code_message_format(self):
|
||||
"""Test that Claude Code message format works correctly."""
|
||||
issue_content = 'Fix the authentication bug'
|
||||
|
||||
formatted_message = CLAUDE_CODE_MESSAGE_FORMAT.format(
|
||||
issue=issue_content,
|
||||
test_command=AIDER_TEST,
|
||||
lint_command=AIDER_LINT,
|
||||
)
|
||||
|
||||
# Verify the issue content is included
|
||||
assert issue_content in formatted_message
|
||||
|
||||
# Verify the test and lint commands are included
|
||||
assert AIDER_TEST in formatted_message
|
||||
assert AIDER_LINT in formatted_message
|
||||
|
||||
# Verify the guidelines are present
|
||||
assert 'Run tests after making changes' in formatted_message
|
||||
assert 'Follow existing code style' in formatted_message
|
||||
assert 'Make minimal, focused changes' in formatted_message
|
||||
assert 'Commit your changes' in formatted_message
|
||||
|
||||
# Verify the structure contains placeholders that got replaced
|
||||
assert '{issue}' not in formatted_message
|
||||
assert '{test_command}' not in formatted_message
|
||||
assert '{lint_command}' not in formatted_message
|
Loading…
Reference in New Issue
Block a user