Compare commits
3 Commits
d196155bf7
...
e9a0719eb2
Author | SHA1 | Date | |
---|---|---|---|
e9a0719eb2 | |||
f306faab16 | |||
727b788d01 |
|
@ -149,20 +149,11 @@ AIDER_LINT = bash_cmd(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
LLM_MESSAGE_FORMAT = """
|
LLM_MESSAGE_FORMAT = """{issue}\nDo not wait for explicit approval before working on code changes."""
|
||||||
{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
|
|
||||||
|
|
||||||
|
#CODE_MODEL = 'ollama/gemma3:4b'
|
||||||
|
CODE_MODEL = None
|
||||||
|
EVALUATOR_MODEL = 'ollama/gemma3:27b'
|
||||||
|
|
||||||
def create_aider_command(issue: str) -> list[str]:
|
def create_aider_command(issue: str) -> list[str]:
|
||||||
l = [
|
l = [
|
||||||
|
@ -171,31 +162,39 @@ def create_aider_command(issue: str) -> list[str]:
|
||||||
'english',
|
'english',
|
||||||
'--no-stream',
|
'--no-stream',
|
||||||
'--no-analytics',
|
'--no-analytics',
|
||||||
|
#'--no-check-update',
|
||||||
'--test-cmd',
|
'--test-cmd',
|
||||||
AIDER_TEST,
|
AIDER_TEST,
|
||||||
'--lint-cmd',
|
'--lint-cmd',
|
||||||
AIDER_LINT,
|
AIDER_LINT,
|
||||||
'--auto-test',
|
'--auto-test',
|
||||||
'--no-auto-lint',
|
'--no-auto-lint',
|
||||||
'--read',
|
|
||||||
'CONVENTIONS.md',
|
|
||||||
'--message',
|
|
||||||
LLM_MESSAGE_FORMAT.format(issue=issue),
|
|
||||||
'--yes',
|
'--yes',
|
||||||
]
|
]
|
||||||
|
|
||||||
for key in secrets.llm_api_keys():
|
for key in secrets.llm_api_keys():
|
||||||
l += ['--api-key', key]
|
l += ['--api-key', key]
|
||||||
|
|
||||||
|
if False:
|
||||||
|
l.append('--read')
|
||||||
|
l.append('CONVENTIONS.md')
|
||||||
|
|
||||||
if True:
|
if True:
|
||||||
l.append('--cache-prompts')
|
l.append('--cache-prompts')
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
l.append('--architect')
|
l.append('--architect')
|
||||||
|
|
||||||
if MODEL:
|
if CODE_MODEL:
|
||||||
l.append('--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
|
return l
|
||||||
|
|
||||||
|
@ -225,6 +224,18 @@ def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> lis
|
||||||
return []
|
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(
|
def push_changes(
|
||||||
repository_config: RepositoryConfig,
|
repository_config: RepositoryConfig,
|
||||||
cwd: Path,
|
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)
|
result = subprocess.run(cmd, check=check, cwd=cwd)
|
||||||
return result.returncode == 0
|
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(
|
def solve_issue_in_repository(
|
||||||
repository_config: RepositoryConfig,
|
repository_config: RepositoryConfig,
|
||||||
tmpdirname: Path,
|
repository_path: Path,
|
||||||
branch_name: str,
|
branch_name: str,
|
||||||
issue_title: str,
|
issue_title: str,
|
||||||
issue_description: str,
|
issue_description: str,
|
||||||
|
@ -319,67 +408,45 @@ def solve_issue_in_repository(
|
||||||
logger.info('### %s #####', issue_title)
|
logger.info('### %s #####', issue_title)
|
||||||
|
|
||||||
# Setup repository
|
# Setup repository
|
||||||
run_cmd(['git', 'clone', repository_config.repo_url(), tmpdirname])
|
run_cmd(['git', 'clone', repository_config.repo_url(), repository_path])
|
||||||
run_cmd(['bash', '-c', AIDER_TEST], tmpdirname)
|
run_cmd(['bash', '-c', AIDER_TEST], repository_path)
|
||||||
run_cmd(['git', 'checkout', repository_config.base_branch], tmpdirname)
|
run_cmd(['git', 'checkout', repository_config.base_branch], repository_path)
|
||||||
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
|
run_cmd(['git', 'checkout', '-b', branch_name], repository_path)
|
||||||
|
|
||||||
# Run initial ruff pass before aider
|
# Run initial ruff pass before aider
|
||||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], repository_path, check=False)
|
||||||
run_cmd(['git', 'add', '.'], tmpdirname)
|
run_cmd(['git', 'add', '.'], repository_path)
|
||||||
run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], tmpdirname, check=False)
|
run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], repository_path, 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 aider
|
# Run aider
|
||||||
issue_content = f'# {issue_title}\n{issue_description}'
|
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:
|
|
||||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
|
||||||
return IssueResolution(False)
|
|
||||||
|
|
||||||
# Auto-fix standard code quality stuff after aider
|
while True:
|
||||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
# Save the commit hash after ruff but before aider
|
||||||
run_cmd(['git', 'add', '.'], tmpdirname)
|
pre_aider_commit = get_head_commit_hash(repository_path)
|
||||||
run_cmd(['git', 'commit', '-m', 'Ruff after aider'], tmpdirname, check=False)
|
|
||||||
|
|
||||||
# Check if aider made any changes beyond the initial ruff pass
|
# Run aider
|
||||||
result = subprocess.run(
|
aider_did_not_crash = issue_solution_round(repository_path, issue_content)
|
||||||
['git', 'diff', pre_aider_commit, 'HEAD', '--name-only'],
|
if not aider_did_not_crash:
|
||||||
check=True,
|
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||||
cwd=tmpdirname,
|
return IssueResolution(False)
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
files_changed = result.stdout.strip()
|
|
||||||
|
|
||||||
if not files_changed and not SKIP_AIDER:
|
# Check if aider made any changes beyond the initial ruff pass
|
||||||
logger.info(
|
if not has_commits_on_branch(repository_path, pre_aider_commit, 'HEAD'):
|
||||||
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
logger.error(
|
||||||
issue_number,
|
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||||
)
|
issue_number,
|
||||||
return IssueResolution(False)
|
)
|
||||||
|
return IssueResolution(False)
|
||||||
|
|
||||||
|
# Verify whether this is a satisfactory solution
|
||||||
|
if verify_solution(repository_path, issue_content):
|
||||||
|
break
|
||||||
|
|
||||||
# Push changes
|
# Push changes
|
||||||
return push_changes(
|
return push_changes(
|
||||||
repository_config,
|
repository_config,
|
||||||
tmpdirname,
|
repository_path,
|
||||||
branch_name,
|
branch_name,
|
||||||
issue_number,
|
issue_number,
|
||||||
issue_title,
|
issue_title,
|
||||||
|
@ -417,10 +484,10 @@ def solve_issues_in_repository(
|
||||||
continue
|
continue
|
||||||
|
|
||||||
branch_name = generate_branch_name(issue_number, title)
|
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(
|
issue_resolution = solve_issue_in_repository(
|
||||||
repository_config,
|
repository_config,
|
||||||
Path(tmpdirname),
|
Path(repository_path),
|
||||||
branch_name,
|
branch_name,
|
||||||
title,
|
title,
|
||||||
issue_description,
|
issue_description,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user