| import asyncio | |
| import dataclasses | |
| import enum | |
| import logging | |
| import time | |
| from collections.abc import Sequence | |
| import bazel_agent | |
| import utils | |
| logger = logging.getLogger(__name__) | |
| # This is the max iteration on the bot side. We will run agent MAX_ITERATIONS times to see if it can give us a fix | |
| # Note that agent has its own max iterations. The difference being that agent iterations iterates on an already made fix | |
| # while we start fresh everytime. | |
| MAX_AGENT_ITERATIONS = 3 | |
| # When agent returns resource exhausted, number of times we retry before giving up. | |
| MAX_AGENT_RESOURCE_EXHAUSTED_TRIES = 3 | |
| class FixTool(enum.Enum): | |
| BANT = "bant" | |
| AI = "ai" | |
| NOT_FIXED = "not fixed" | |
| @dataclasses.dataclass | |
| class RepairSummaryRecord: | |
| # Commit that was repaired | |
| commit: str | |
| # What fixed it: 'bant', 'ai', 'not fixed' | |
| fixed_by: FixTool | |
| # timestmap | |
| timestamp: float | |
| class BazelRepairBot: | |
| def __init__( | |
| self, | |
| command_processor: utils.CommandProcessor, | |
| git_repo: utils.LocalGitRepo, | |
| creds: utils.CredentialManager, | |
| build_processor: utils.BuildProcessor, | |
| poll_interval, | |
| ): | |
| # Injectables | |
| self.command_processor = command_processor | |
| self.creds = creds | |
| self.git_repo = git_repo | |
| self.build_processor = build_processor | |
| # Simple variables | |
| self.last_processed_build = 0 | |
| self.last_processed_state: utils.BuildState = utils.BuildState.UNKNOWN | |
| self.last_processed_sha: str = "" | |
| self.poll_interval = poll_interval | |
| # Branch containing fixes that have not been merged | |
| self.active_fix_branch: str | None = None | |
| def reset_active_fix_branch(self) -> None: | |
| self.active_fix_branch = None | |
| def wait(self, seconds): | |
| """Sleeps for a given number of seconds""" | |
| time.sleep(seconds) | |
| def process_failure_with_bant(self, build_data: utils.BuildInfo) -> bool: | |
| base_sha = self.git_repo.repo.head.commit.hexsha | |
| if not self.command_processor.run_bant(targets=build_data.failed_targets): | |
| logger.warning("Running bant failed.") | |
| self.git_repo.reset_hard(base_sha) | |
| return False | |
| if not self.validate_before_publishing(): | |
| logger.info("Validation before publishing failed.") | |
| self.git_repo.reset_hard(base_sha) | |
| return False | |
| self.git_repo.commit( | |
| f"[Bazel] Fix build for {build_data.commit[:7]}\n\n" | |
| f"Buildkite error link: {build_data.buildkite_url}" | |
| ) | |
| if not self.git_repo.push_fix( | |
| build_data, close_existing_prs=not self.active_fix_branch | |
| ): | |
| logger.warning("Failed to publish bant fix.") | |
| return False | |
| return True | |
| def repair_build(self, build: utils.BuildInfo) -> bool: | |
| if build.state != utils.BuildState.FAILED: | |
| logger.info(f"Build for {build.commit} is not failed. Nothing to repair.") | |
| return False | |
| record = RepairSummaryRecord(build.commit, "", time.time()) | |
| if self.process_failure_with_bant(build): | |
| logger.info(f"Successfully repaired build for {build.commit}) using bant.") | |
| record.fixed_by = FixTool.BANT | |
| elif self.process_failure_with_ai(build): | |
| logger.info( | |
| f"Successfully repaired build for {build.commit} using AI agent." | |
| ) | |
| record.fixed_by = FixTool.AI | |
| else: | |
| record.fixed_by = FixTool.NOT_FIXED | |
| return record.fixed_by != FixTool.NOT_FIXED | |
| def run_buildifier_on_changed_files(self) -> bool: | |
| repo = self.git_repo.repo | |
| changed_files = repo.git.diff(name_only=True).splitlines() | |
| changed_files.extend(repo.untracked_files) | |
| return self.command_processor.run_buildifier(files=changed_files) | |
| def validate_before_publishing(self) -> bool: | |
| if not self.git_repo.is_repo_dirty(untracked_files=True): | |
| logger.info("validate: no changes detected.") | |
| return False | |
| bazel_build = self.command_processor.run_bazel_build() | |
| if not bazel_build.success: | |
| logger.info("validate: build fails.") | |
| logger.info(bazel_build.stderr) | |
| return False | |
| if not self.run_buildifier_on_changed_files(): | |
| logger.info("validate: buildifier failed.") | |
| return False | |
| # Check bazel build again after running buildifier | |
| bazel_build = self.command_processor.run_bazel_build() | |
| if not bazel_build.success: | |
| logger.info("validate: build fails after buildifier.") | |
| logger.info(bazel_build.stderr) | |
| return False | |
| return True | |
| def process_failure_with_ai(self, build_data: utils.BuildInfo) -> bool: | |
| logger.info("Invoking AI Agent...") | |
| attempt_num = 0 | |
| agent_exhausted_tries = 0 | |
| past_fixes = [] | |
| base_sha = self.git_repo.repo.head.commit.hexsha | |
| # Push an empty branch to indicate that AI fix is in progress | |
| self.git_repo.commit( | |
| f"AI is working on fixing build for {build_data.commit[:7]}. Check back later!\n\n" | |
| f"Buildkite error link: {build_data.buildkite_url}" | |
| ) | |
| self.git_repo.push_fix(build_data, False) | |
| while True: | |
| if attempt_num == MAX_AGENT_ITERATIONS: | |
| logger.info( | |
| f"Reached max AI agent attempts ({MAX_AGENT_ITERATIONS}) for commit {build_data.commit}. Giving up." | |
| ) | |
| break | |
| self.git_repo.reset_hard(base_sha) | |
| # For local testing, users must use their personal access tokens | |
| access_token = ( | |
| self.git_repo.fork_github_integration.get_access_token( | |
| self.git_repo.gh_fork_installation.id | |
| ) | |
| if self.creds.use_github_app | |
| else self.creds.gh_fork_user_token | |
| ) | |
| # We pass the local path so the agent can read files, and the commit to analyze | |
| agent_result = asyncio.run( | |
| bazel_agent.query_agent( | |
| commit_sha=build_data.commit, | |
| cmd_processor=self.command_processor, | |
| past_fixes=past_fixes[:], | |
| github_token=access_token, | |
| ) | |
| ) | |
| logger.info("------------------") | |
| logger.info(f"AI Agent attempt #{attempt_num} summary") | |
| logger.info(agent_result.summary) | |
| logger.info("------------------") | |
| if agent_result.status == bazel_agent.AgentErrors.AGENT_RESOURCE_EXHAUSTED: | |
| self.wait(self.poll_interval * 5) | |
| agent_exhausted_tries += 1 | |
| if agent_exhausted_tries > MAX_AGENT_RESOURCE_EXHAUSTED_TRIES: | |
| logger.warning( | |
| f"Giving up commit {build_data.commit} after too many resource exhausted errors" | |
| ) | |
| break | |
| continue | |
| elif ( | |
| agent_result.status != bazel_agent.AgentErrors.SUCCESS | |
| and self.git_repo.is_repo_dirty(untracked_files=True) | |
| ): | |
| # Get dirty status of the repo in storage | |
| past_fixes.append(self.git_repo.diff()) | |
| elif agent_result.status == bazel_agent.AgentErrors.SUCCESS: | |
| break | |
| attempt_num += 1 | |
| if not self.validate_before_publishing(): | |
| logger.info("Validation before publishing failed.") | |
| self.git_repo.reset_hard(base_sha) | |
| self.git_repo.commit( | |
| f"AI failed to fix the build for {build_data.commit[:7]}. You " | |
| f"will need to fix it manually\n\n" | |
| f"Buildkite error link: {build_data.buildkite_url}" | |
| ) | |
| self.git_repo.push_fix(build_data, False) | |
| return False | |
| self.git_repo.commit( | |
| f"Fix Bazel build for {build_data.commit[:7]}\n\n" | |
| f"Buildkite error link: {build_data.buildkite_url}" | |
| ) | |
| if not self.git_repo.push_fix( | |
| build_data, | |
| self.git_repo.can_create_pr, | |
| close_existing_prs=not self.active_fix_branch, | |
| ): | |
| logger.warning( | |
| f"Failed to publish AI changes for commit: {build_data.commit}" | |
| ) | |
| return False | |
| return True | |
| def set_state_from_latest_build(self) -> bool: | |
| logger.info("Initializing state with the latest build.") | |
| latest_build = self.build_processor.get_latest_build_status() | |
| if latest_build: | |
| self.last_processed_sha = latest_build.commit | |
| self.last_processed_state = latest_build.state | |
| return True | |
| logger.error("Could not init state from latest build.") | |
| return False | |
| def get_build_info_for_commit(self, commit_sha: str) -> utils.BuildInfo | None: | |
| # Build 1: Bazel build with a clean checkout of commit_sha | |
| # Build 2: Bazel build of commit_sha + active_fix_branch containing fixes for previous errors | |
| logger.info( | |
| f"Finding build state for commit {commit_sha} locally (build 1: clean commit)..." | |
| ) | |
| current_build = utils.BuildInfo(commit=commit_sha) | |
| self.git_repo.checkout_commit(commit_sha) | |
| build_result = self.command_processor.run_bazel_build() | |
| if build_result.success: | |
| logger.info( | |
| f"Build 1 for commit {commit_sha} passed. Resetting active fix branch." | |
| ) | |
| self.reset_active_fix_branch() | |
| current_build.state = utils.BuildState.PASSED | |
| return current_build | |
| logger.info(f"Build 1 for commit {commit_sha} failed.") | |
| current_build.failed_targets = utils.parse_targets(build_result.stderr) | |
| if not self.active_fix_branch: | |
| current_build.state = utils.BuildState.FAILED | |
| return current_build | |
| logger.info( | |
| f"Rebasing active fix branch {self.active_fix_branch} on top of commit {commit_sha}..." | |
| ) | |
| if not self.git_repo.rebase_branch(self.active_fix_branch, commit_sha): | |
| logger.warning( | |
| f"Failed to rebase active fix branch {self.active_fix_branch} on top of commit {commit_sha}." | |
| ) | |
| # Failed git rebase operation -> not buildable | |
| # Let's reset the active fix branch so next revision doesn't have any rebase | |
| self.reset_active_fix_branch() | |
| current_build.state = utils.BuildState.UNKNOWN | |
| return current_build | |
| logger.info( | |
| f"Finding build state for commit {commit_sha} locally (build 2: with active fix branch {self.active_fix_branch})..." | |
| ) | |
| build_result2 = self.command_processor.run_bazel_build() | |
| if build_result2.success: | |
| logger.info( | |
| f"Build 2 for commit {commit_sha} with fix branch {self.active_fix_branch} passed." | |
| ) | |
| current_build.state = utils.BuildState.PASSED | |
| current_build.failed_targets = [] | |
| else: | |
| logger.info( | |
| f"Build 2 for commit {commit_sha} with fix branch {self.active_fix_branch} failed." | |
| ) | |
| current_build.state = utils.BuildState.FAILED | |
| current_build.failed_targets = utils.parse_targets(build_result2.stderr) | |
| return current_build | |
| def run(self) -> None: | |
| while True: | |
| # On first run (or state reset), initialize with the latest build number. | |
| if not self.last_processed_sha: | |
| if not self.set_state_from_latest_build(): | |
| continue | |
| builds_to_process: Sequence[ | |
| utils.BuildInfo | |
| ] = self.build_processor.get_builds_to_process(self.last_processed_sha) | |
| if not builds_to_process: | |
| logger.debug("No new builds to process. Sleeping ...") | |
| self.wait(self.poll_interval) | |
| continue | |
| logger.info(f"Found {len(builds_to_process)} new builds to process.") | |
| for build in builds_to_process: | |
| if not build: | |
| continue | |
| if build.state == utils.BuildState.UNKNOWN: | |
| current_build = self.get_build_info_for_commit(build.commit) | |
| if current_build: | |
| current_build.pr_number = build.pr_number | |
| else: | |
| current_build = build | |
| logger.info( | |
| f"Processing build {current_build.commit} with state '{current_build.state}'." | |
| ) | |
| if ( | |
| current_build.state == utils.BuildState.RUNNING | |
| or current_build.state == utils.BuildState.SCHEDULED | |
| ): | |
| # TODO: Remove: We should never run into this now since we are building locally | |
| logger.debug( | |
| f"Build for {current_build.commit} is still running. Waiting for completion." | |
| ) | |
| # Stop processing current list of builds for now. | |
| break | |
| elif current_build.state == utils.BuildState.PASSED: | |
| logger.info( | |
| f"Build for {current_build.commit} passed. Nothing to do." | |
| ) | |
| elif current_build.state == utils.BuildState.FAILED: | |
| if self.last_processed_state == utils.BuildState.PASSED: | |
| logger.info( | |
| f"Detected 'passed' -> '{current_build.state}' transition for {current_build.commit}). Repairing ..." | |
| ) | |
| branch_name = self.git_repo.create_branch_for_fix( | |
| current_build.commit | |
| ) | |
| if self.repair_build(current_build): | |
| self.active_fix_branch = branch_name | |
| current_build.state = utils.BuildState.PASSED | |
| else: | |
| logger.warning( | |
| f"Failed to repair build #{current_build.commit} (commit: {current_build.commit}). Giving up and waiting for user to fix it manually." | |
| ) | |
| self.reset_active_fix_branch() | |
| else: | |
| logger.info( | |
| f"{self.last_processed_sha} also not passing. Waiting for transition from passed->failed state." | |
| ) | |
| else: | |
| # Unknown states. Skip this. | |
| logger.warning( | |
| f"Build for {current_build.commit} is in unknown state ❌: ('{current_build.state}'). Skipping this." | |
| ) | |
| # Update state for the next iteration. | |
| self.last_processed_state = current_build.state | |
| self.last_processed_sha = current_build.commit | |
| self.wait(self.poll_interval) |