From 2b68790bce8386f5c540f5f22361802d6c8d9652 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Tue, 24 Jun 2025 13:33:15 +0200 Subject: [PATCH] Add method execute_task for synchronous execution and apply it in demo script --- scripts/demo_run_tools.py | 4 ++-- src/serena/agent.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/scripts/demo_run_tools.py b/scripts/demo_run_tools.py index 23726f12..8822db7a 100644 --- a/scripts/demo_run_tools.py +++ b/scripts/demo_run_tools.py @@ -20,10 +20,10 @@ class InMemorySerenaConfig(SerenaConfigBase): if __name__ == "__main__": - # project_path = str(Path("test") / "resources" / "repos" / "python" / "test_repo") agent = SerenaAgent(project=REPO_ROOT) # apply a tool find_refs_tool = agent.get_tool(FindReferencingSymbolsTool) print("Finding the symbol 'SyncLanguageServer'\n") - pprint(json.loads(find_refs_tool.apply(name_path="SyncLanguageServer", relative_path="src/multilspy/language_server.py"))) + result = agent.execute_task(lambda: find_refs_tool.apply(name_path="SolidLanguageServer", relative_path="src/solidlsp/ls.py")) + pprint(json.loads(result)) diff --git a/src/serena/agent.py b/src/serena/agent.py index 9b7192da..7da88e1f 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -63,6 +63,7 @@ if TYPE_CHECKING: log = logging.getLogger(__name__) TTool = TypeVar("TTool", bound="Tool") +T = TypeVar("T") SUCCESS_RESULT = "OK" DEFAULT_TOOL_TIMEOUT: float = 240 @@ -957,6 +958,8 @@ class SerenaAgent: def issue_task(self, task: Callable[[], Any], name: str | None = None) -> Future: """ Issue a task to the executor for asynchronous execution. + It is ensured that tasks are executed in the order they are issued, one after another. + :param task: the task to execute :param name: the name of the task for logging purposes; if None, use the task function's name :return: a Future object representing the execution of the task @@ -972,6 +975,17 @@ class SerenaAgent: log.info(f"Scheduling {task_name}") return self._task_executor.submit(task_execution_wrapper) + def execute_task(self, task: Callable[[], T]) -> T: + """ + Executes the given task synchronously via the agent's task executor. + This is useful for tasks that need to be executed immediately and whose results are needed right away. + + :param task: the task to execute + :return: the result of the task execution + """ + future = self.issue_task(task) + return future.result() + def _activate_project(self, project: Project) -> None: log.info(f"Activating {project.project_name} at {project.project_root}") self._active_project = project