Compare commits
No commits in common. "e9a0719eb259ee04a98c6cbfac26a79d60159592" and "d196155bf7b5c9883a7cb8878f084c762759dc0b" have entirely different histories.
e9a0719eb2
...
d196155bf7
|
@ -149,11 +149,20 @@ AIDER_LINT = bash_cmd(
|
|||
)
|
||||
|
||||
|
||||
LLM_MESSAGE_FORMAT = """{issue}\nDo not wait for explicit approval before working on code changes."""
|
||||
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
|
||||
|
||||
#CODE_MODEL = 'ollama/gemma3:4b'
|
||||
CODE_MODEL = None
|
||||
EVALUATOR_MODEL = 'ollama/gemma3:27b'
|
||||
|
||||
def create_aider_command(issue: str) -> list[str]:
|
||||
l = [
|
||||
|
@ -162,39 +171,31 @@ 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 CODE_MODEL:
|
||||
if 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))
|
||||
l.append(MODEL)
|
||||
|
||||
return l
|
||||
|
||||
|
@ -224,18 +225,6 @@ 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,
|
||||
|
@ -314,91 +303,13 @@ 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
|
||||
|
||||
# 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()
|
||||
SKIP_AIDER = False
|
||||
|
||||
|
||||
def solve_issue_in_repository(
|
||||
repository_config: RepositoryConfig,
|
||||
repository_path: Path,
|
||||
tmpdirname: Path,
|
||||
branch_name: str,
|
||||
issue_title: str,
|
||||
issue_description: str,
|
||||
|
@ -408,45 +319,67 @@ def solve_issue_in_repository(
|
|||
logger.info('### %s #####', issue_title)
|
||||
|
||||
# Setup repository
|
||||
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_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 initial ruff pass before 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', 'Initial ruff pass'], repository_path, check=False)
|
||||
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 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:
|
||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||
return IssueResolution(False)
|
||||
|
||||
while True:
|
||||
# Save the commit hash after ruff but before aider
|
||||
pre_aider_commit = get_head_commit_hash(repository_path)
|
||||
# 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)
|
||||
|
||||
# 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)
|
||||
# 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()
|
||||
|
||||
# Check if aider 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',
|
||||
issue_number,
|
||||
)
|
||||
return IssueResolution(False)
|
||||
|
||||
# Verify whether this is a satisfactory solution
|
||||
if verify_solution(repository_path, issue_content):
|
||||
break
|
||||
if not files_changed and not SKIP_AIDER:
|
||||
logger.info(
|
||||
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||
issue_number,
|
||||
)
|
||||
return IssueResolution(False)
|
||||
|
||||
# Push changes
|
||||
return push_changes(
|
||||
repository_config,
|
||||
repository_path,
|
||||
tmpdirname,
|
||||
branch_name,
|
||||
issue_number,
|
||||
issue_title,
|
||||
|
@ -484,10 +417,10 @@ def solve_issues_in_repository(
|
|||
continue
|
||||
|
||||
branch_name = generate_branch_name(issue_number, title)
|
||||
with tempfile.TemporaryDirectory() as repository_path:
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
issue_resolution = solve_issue_in_repository(
|
||||
repository_config,
|
||||
Path(repository_path),
|
||||
Path(tmpdirname),
|
||||
branch_name,
|
||||
title,
|
||||
issue_description,
|
||||
|
|
Loading…
Reference in New Issue
Block a user