Compare commits

..

3 Commits

Author SHA1 Message Date
e9a0719eb2 Stuff
Some checks failed
Run Python tests (through Pytest) / Test (push) Failing after 26s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s
2025-04-21 12:15:13 +02:00
f306faab16 Check the current code quality 2025-04-21 11:02:21 +02:00
727b788d01 Simplify 2025-04-21 10:21:24 +02:00

View File

@ -149,20 +149,11 @@ AIDER_LINT = bash_cmd(
)
LLM_MESSAGE_FORMAT = """
{issue}
# Solution Details
For code tasks:
1. Create a plan for how to solve the issue.
2. Write unit tests that proves that your solution works.
3. Then, solve the issue by writing the required code.
"""
MODEL = None
LLM_MESSAGE_FORMAT = """{issue}\nDo not wait for explicit approval before working on code changes."""
#CODE_MODEL = 'ollama/gemma3:4b'
CODE_MODEL = None
EVALUATOR_MODEL = 'ollama/gemma3:27b'
def create_aider_command(issue: str) -> list[str]:
l = [
@ -171,31 +162,39 @@ def create_aider_command(issue: str) -> list[str]:
'english',
'--no-stream',
'--no-analytics',
#'--no-check-update',
'--test-cmd',
AIDER_TEST,
'--lint-cmd',
AIDER_LINT,
'--auto-test',
'--no-auto-lint',
'--read',
'CONVENTIONS.md',
'--message',
LLM_MESSAGE_FORMAT.format(issue=issue),
'--yes',
]
for key in secrets.llm_api_keys():
l += ['--api-key', key]
if False:
l.append('--read')
l.append('CONVENTIONS.md')
if True:
l.append('--cache-prompts')
if False:
l.append('--architect')
if MODEL:
if CODE_MODEL:
l.append('--model')
l.append(MODEL)
l.append(CODE_MODEL)
if CODE_MODEL.startswith('ollama/'):
l.append('--auto-lint')
if True:
l.append('--message')
l.append(LLM_MESSAGE_FORMAT.format(issue=issue))
return l
@ -225,6 +224,18 @@ def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> lis
return []
def get_diff(cwd: Path, base_branch: str, current_branch: str) -> str:
result = subprocess.run(
['git', 'diff', f'{base_branch}..{current_branch}', '--pretty=format:%s'],
check=True,
cwd=cwd,
capture_output=True,
text=True,
)
return result.stdout.strip()
def push_changes(
repository_config: RepositoryConfig,
cwd: Path,
@ -303,13 +314,91 @@ def run_cmd(cmd: list[str], cwd: Path | None = None, check=True) -> bool:
result = subprocess.run(cmd, check=check, cwd=cwd)
return result.returncode == 0
def issue_solution_round(repository_path, issue_content):
# Primary Aider command
aider_command = create_aider_command(issue_content)
print(aider_command)
aider_did_not_crash = run_cmd(
aider_command,
repository_path,
check=False,
)
if not aider_did_not_crash:
return aider_did_not_crash
SKIP_AIDER = False
# 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 run_ollama(cwd: Path, texts: list[str]) -> str:
cmd = ['ollama', 'run', EVALUATOR_MODEL.removeprefix('ollama/')]
print(cmd)
process = subprocess.Popen(
cmd,
cwd=cwd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
stdout, stderr = process.communicate('\n'.join(texts))
print(stdout)
return stdout
def parse_yes_no_answer(text: str) -> bool | None:
text = text.lower().strip()
words = text.split('\n \t.,?-')
print(words)
if words[-1] in {'yes', 'agree'}:
return True
if words[-1] in {'no', 'disagree'}:
return False
return None
def run_ollama_and_get_yes_or_no(cwd, initial_texts: list[str]) -> bool:
texts = list(initial_texts)
texts.append('Think through your answer.')
while True:
response = run_ollama(cwd, texts)
yes_or_no = parse_yes_no_answer(response)
if yes_or_no is not None:
return yes_or_no
else:
texts.append(response)
texts.append('Please answer either "yes" or "no".')
def verify_solution(repository_path: Path, issue_content: str) -> bool:
summary = run_ollama(
repository_path,
['Concisely summarize following changeset',
get_diff(repository_path, 'HEAD', 'main')
])
return run_ollama_and_get_yes_or_no(
repository_path,
['Does this changeset accomplish the entire task?',
'# Change set',
summary,
'# Issue',
issue_content,
])
def get_head_commit_hash(repository_path: Path) -> str:
return subprocess.run(
['git', 'rev-parse', 'HEAD'],
check=True,
cwd=repository_path,
capture_output=True,
text=True,
).stdout.strip()
def solve_issue_in_repository(
repository_config: RepositoryConfig,
tmpdirname: Path,
repository_path: Path,
branch_name: str,
issue_title: str,
issue_description: str,
@ -319,67 +408,45 @@ def solve_issue_in_repository(
logger.info('### %s #####', issue_title)
# Setup repository
run_cmd(['git', 'clone', repository_config.repo_url(), tmpdirname])
run_cmd(['bash', '-c', AIDER_TEST], tmpdirname)
run_cmd(['git', 'checkout', repository_config.base_branch], tmpdirname)
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
run_cmd(['git', 'clone', repository_config.repo_url(), repository_path])
run_cmd(['bash', '-c', AIDER_TEST], repository_path)
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_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
run_cmd(['git', 'add', '.'], tmpdirname)
run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], tmpdirname, check=False)
# Save the commit hash after ruff but before aider
result = subprocess.run(
['git', 'rev-parse', 'HEAD'],
check=True,
cwd=tmpdirname,
capture_output=True,
text=True,
)
pre_aider_commit = result.stdout.strip()
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
issue_content = f'# {issue_title}\n{issue_description}'
if not SKIP_AIDER:
succeeded = run_cmd(
create_aider_command(issue_content),
tmpdirname,
check=False,
)
else:
logger.warning('Skipping aider command (for testing)')
succeeded = True
if not succeeded:
while True:
# Save the commit hash after ruff but before aider
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)
return IssueResolution(False)
# Auto-fix standard code quality stuff after aider
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
run_cmd(['git', 'add', '.'], tmpdirname)
run_cmd(['git', 'commit', '-m', 'Ruff after aider'], tmpdirname, check=False)
# Check if aider made any changes beyond the initial ruff pass
result = subprocess.run(
['git', 'diff', pre_aider_commit, 'HEAD', '--name-only'],
check=True,
cwd=tmpdirname,
capture_output=True,
text=True,
)
files_changed = result.stdout.strip()
if not files_changed and not SKIP_AIDER:
logger.info(
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',
issue_number,
)
return IssueResolution(False)
# Verify whether this is a satisfactory solution
if verify_solution(repository_path, issue_content):
break
# Push changes
return push_changes(
repository_config,
tmpdirname,
repository_path,
branch_name,
issue_number,
issue_title,
@ -417,10 +484,10 @@ def solve_issues_in_repository(
continue
branch_name = generate_branch_name(issue_number, title)
with tempfile.TemporaryDirectory() as tmpdirname:
with tempfile.TemporaryDirectory() as repository_path:
issue_resolution = solve_issue_in_repository(
repository_config,
Path(tmpdirname),
Path(repository_path),
branch_name,
title,
issue_description,