From 6cfae57cd3d855bd52c83f7e56fbf2fb5c6d3072 Mon Sep 17 00:00:00 2001 From: mijauexe Date: Mon, 5 May 2025 17:15:13 +0200 Subject: [PATCH] Add redo task functionality --- core/agents/developer.py | 10 +++++++++ core/agents/orchestrator.py | 17 +++++++++++++-- core/agents/troubleshooter.py | 28 ++++++++++++++++++++++--- core/db/models/project_state.py | 25 +++++++++++++++++++++- core/prompts/developer/breakdown.prompt | 8 +++++++ core/state/state_manager.py | 3 +++ 6 files changed, 85 insertions(+), 6 deletions(-) diff --git a/core/agents/developer.py b/core/agents/developer.py index b2082e4246..58bc73e2df 100644 --- a/core/agents/developer.py +++ b/core/agents/developer.py @@ -230,6 +230,15 @@ class Developer(ChatWithBreakdownMixin, RelevantFilesMixin, BaseAgent): ): related_api_endpoints = [] + redo_task_user_feedback = None + + if ( + self.next_state + and self.next_state.current_task + and self.next_state.current_task.get("redo_human_instructions", None) is not None + ): + redo_task_user_feedback = self.next_state.current_task["redo_human_instructions"] + convo = AgentConvo(self).template( "breakdown", task=current_task, @@ -237,6 +246,7 @@ class Developer(ChatWithBreakdownMixin, RelevantFilesMixin, BaseAgent): current_task_index=current_task_index, docs=self.current_state.docs, related_api_endpoints=related_api_endpoints, + redo_task_user_feedback=redo_task_user_feedback, ) response: str = await llm(convo) convo.assistant(response) diff --git a/core/agents/orchestrator.py b/core/agents/orchestrator.py index a8abf7ae27..9e9cf19ad2 100644 --- a/core/agents/orchestrator.py +++ b/core/agents/orchestrator.py @@ -72,12 +72,25 @@ class Orchestrator(BaseAgent, GitMixin): await self.set_favicon() await self.enable_debugger() - # TODO: consider refactoring this into two loop; the outer with one iteration per comitted step, + # TODO: consider refactoring this into two loop; the outer with one iteration per committed step, # and the inner which runs the agents for the current step until they're done. This would simplify # handle_done() and let us do other per-step processing (eg. describing files) in between agent runs. while True: - await self.update_stats() + # If the task is marked as "redo_human_instructions", we need to reload the project at the state before the current task breakdown + if ( + self.current_state.current_task + and self.current_state.current_task.get("redo_human_instructions", None) is not None + ): + redo_human_instructions = self.current_state.current_task["redo_human_instructions"] + project_state = await self.state_manager.get_project_state_for_redo_task(self.current_state) + + if project_state is not None: + await self.state_manager.load_project( + branch_id=project_state.branch_id, step_index=project_state.step_index + ) + self.next_state.current_task["redo_human_instructions"] = redo_human_instructions + await self.update_stats() agent = self.create_agent(response) # In case where agent is a list, run all agents in parallel. diff --git a/core/agents/troubleshooter.py b/core/agents/troubleshooter.py index 48ca70ac0d..4183073097 100644 --- a/core/agents/troubleshooter.py +++ b/core/agents/troubleshooter.py @@ -86,11 +86,25 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles # use "current_iteration" here last_iteration = self.current_state.iterations[-1] if len(self.current_state.iterations) >= 3 else None - should_iterate, is_loop, bug_report, change_description = await self.get_user_feedback( + should_iterate, is_loop, should_redo, bug_report, change_description = await self.get_user_feedback( run_command, user_instructions, last_iteration is not None, ) + + if should_redo: + # ask user to provide more info + task_redo_info_question = await self.ask_question( + "Please provide more information about the task you want to redo", + buttons_only=False, + ) + + if task_redo_info_question.text: + self.next_state.current_task["redo_human_instructions"] = task_redo_info_question.text + self.next_state.current_task["redo_task_id"] = self.current_state.current_task["id"] + + return AgentResponse.done(self) + if not should_iterate: # User tested and reported no problems, we're done with the task return await self.complete_task() @@ -235,7 +249,7 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles run_command: str, user_instructions: str, last_iteration: Optional[dict], - ) -> tuple[bool, bool, str, str]: + ) -> tuple[bool, bool, bool, str, str]: """ Ask the user to test the app and provide feedback. @@ -247,6 +261,8 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles If "is_loop" is True, Pythagora is stuck in a loop and needs to consider alternative solutions. + If "should_redo" is True, the user wants to redo the task and we need to reset the task and start over. + The last element in the tuple is the user feedback, which may be empty if the user provided no feedback (eg. if they just clicked on "Continue" or "Start Pair Programming"). """ @@ -257,6 +273,7 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles is_loop = False should_iterate = True + should_redo = False extra_info = "restart_app" if not self.current_state.iterations else None while True: @@ -273,6 +290,7 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles "continue": "Everything works", "change": "I want to make a change", "bug": "There is an issue", + "redo": "Redo task", } user_response = await self.ask_question( @@ -285,6 +303,10 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles ) extra_info = None + if user_response.button == "redo": + should_redo = True + break + if user_response.button == "continue" or user_response.cancelled: should_iterate = False break @@ -314,7 +336,7 @@ class Troubleshooter(ChatWithBreakdownMixin, IterationPromptMixin, RelevantFiles await self.get_relevant_files_parallel(user_feedback=bug_report) break - return should_iterate, is_loop, bug_report, change_description + return should_iterate, is_loop, should_redo, bug_report, change_description def try_next_alternative_solution(self, user_feedback: str, user_feedback_qa: list[str]) -> AgentResponse: """ diff --git a/core/db/models/project_state.py b/core/db/models/project_state.py index 1190d0a404..74841c053f 100644 --- a/core/db/models/project_state.py +++ b/core/db/models/project_state.py @@ -3,7 +3,7 @@ from datetime import datetime from typing import TYPE_CHECKING, Optional, Union from uuid import UUID, uuid4 -from sqlalchemy import ForeignKey, UniqueConstraint, delete, inspect, select +from sqlalchemy import ForeignKey, UniqueConstraint, and_, delete, inspect, select from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.orm.attributes import flag_modified @@ -558,3 +558,26 @@ class ProjectState(Base): :return: True if the current epic is a feature, False otherwise. """ return self.epics and self.current_epic and self.current_epic.get("source") == "feature" + + @staticmethod + async def get_state_for_redo_task(session: AsyncSession, project_state: "ProjectState") -> Optional["ProjectState"]: + states_result = await session.execute( + select(ProjectState).where( + and_( + ProjectState.step_index <= project_state.step_index, + ProjectState.branch_id == project_state.branch_id, + ) + ) + ) + + result = states_result.scalars().all() + + result = sorted(result, key=lambda x: x.step_index, reverse=True) + for state in result: + if state.tasks: + for task in state.tasks: + if task.get("id") == project_state.current_task.get("id") and task.get("instructions") is None: + if task.get("status") == TaskStatus.TODO: + return state + + return None diff --git a/core/prompts/developer/breakdown.prompt b/core/prompts/developer/breakdown.prompt index e1ea9a4d70..1d89433279 100644 --- a/core/prompts/developer/breakdown.prompt +++ b/core/prompts/developer/breakdown.prompt @@ -48,6 +48,14 @@ We've broken the development of this {% if state.epics|length > 1 %}feature{% el You are currently working on task #{{ current_task_index + 1 }} with the following description: ``` {{ task.description }} + +{% if redo_task_user_feedback is defined and redo_task_user_feedback %} +You tried implementing this task before but you were unsuccessful. Here is what a human developer told you about what to watch out for while you're implementing this task so you don't make the same mistakes again: +---START_OF_USER_FEEDBACK--- +{{ redo_task_user_feedback }} +---END_OF_USER_FEEDBACK--- +{% endif %} + ``` {% if related_api_endpoints|length > 0 %} In this task, you need to focus on implementing the following endpoints:{% for api in related_api_endpoints %}{{ "`" ~ api.endpoint ~ "`" }}{% if not loop.last %},{% endif %}{% endfor %} diff --git a/core/state/state_manager.py b/core/state/state_manager.py index 93b221add0..841264d16c 100644 --- a/core/state/state_manager.py +++ b/core/state/state_manager.py @@ -94,6 +94,9 @@ class StateManager: async def get_branches_for_project_id(self, project_id: UUID) -> list[Branch]: return await Project.get_branches_for_project_id(self.current_session, project_id) + async def get_project_state_for_redo_task(self, project_state: ProjectState) -> Optional[ProjectState]: + return await ProjectState.get_state_for_redo_task(self.current_session, project_state) + async def find_user_input(self, project_state, branch_id) -> Optional[list["UserInput"]]: return await UserInput.find_user_inputs(self.current_session, project_state, branch_id)