From 0def7aaecc3e90114eed9f34bce7e036f28bc587 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Sat, 19 Jul 2025 22:16:50 +0200 Subject: [PATCH] Remove obsolete flag returned by ProjectConfig.load which was never correctly returned anyway (due to a logic error) --- src/serena/agent.py | 17 ++++++----------- src/serena/config/serena_config.py | 15 ++++----------- src/serena/project.py | 5 ++--- src/serena/tools/config_tools.py | 16 +++++----------- 4 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/serena/agent.py b/src/serena/agent.py index 09ae488..7c4b200 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -399,17 +399,17 @@ class SerenaAgent: if self._project_activation_callback is not None: self._project_activation_callback() - def activate_project_from_path_or_name(self, project_root_or_name: str) -> tuple[Project, bool, bool]: + def activate_project_from_path_or_name(self, project_root_or_name: str) -> tuple[Project, bool]: """ Activate a project from a path or a name. If the project was already registered, it will just be activated. If it was not registered, the project will be registered and activated. After that, the project can be activated again by name (not just by path). - :return: a tuple of the project instance and two booleans indicating if a new project was added and if a new project configuration for the - added project was generated. + + :return: a tuple of the project instance and a Boolean indicating whether the project was newly + created """ new_project_generated = False - new_project_config_generated = False project_instance: Project | None = self.serena_config.get_project(project_root_or_name) if project_instance is not None: log.info(f"Found registered project {project_instance.project_name} at path {project_instance.project_root}.") @@ -419,16 +419,11 @@ class SerenaAgent: f"Project '{project_root_or_name}' not found: Not a valid project name or directory. " f"Existing project names: {self.serena_config.project_names}" ) - project_instance, new_project_config_generated = self.serena_config.add_project_from_path(project_root_or_name) + project_instance = self.serena_config.add_project_from_path(project_root_or_name) new_project_generated = True log.info(f"Added new project {project_instance.project_name} for path {project_instance.project_root}.") - if new_project_config_generated: - log.info( - f"Note: A new project configuration with language {project_instance.project_config.language.value} " - f"was autogenerated since no project configuration was found in {project_root_or_name}." - ) self._activate_project(project_instance) - return project_instance, new_project_generated, new_project_config_generated + return project_instance, new_project_generated def get_active_tool_classes(self) -> list[type["Tool"]]: """ diff --git a/src/serena/config/serena_config.py b/src/serena/config/serena_config.py index 94b42ab..3264d25 100644 --- a/src/serena/config/serena_config.py +++ b/src/serena/config/serena_config.py @@ -438,15 +438,13 @@ class SerenaConfig(ToolInclusionDefinition, ToStringMixin): return project return None - def add_project_from_path(self, project_root: Path | str) -> tuple["Project", bool]: + def add_project_from_path(self, project_root: Path | str) -> "Project": """ Add a project to the Serena configuration from a given path. Will raise a FileExistsError if a project already exists at the path. :param project_root: the path to the project to add - :return: the project that was added and a boolean indicating whether a new project configuration was generated and - saved to disk. It may be that no new project configuration was generated if the project configuration already - exists on disk but the project itself was not added yet to the Serena configuration. + :return: the project that was added """ from ..project import Project @@ -462,18 +460,13 @@ class SerenaConfig(ToolInclusionDefinition, ToStringMixin): f"Project with path {project_root} was already added with name '{already_registered_project.project_name}'." ) - try: - project_config = ProjectConfig.load(project_root) - new_project_config_generated = False - except FileNotFoundError: - project_config = ProjectConfig.autogenerate(project_root, save_to_disk=True) - new_project_config_generated = True + project_config = ProjectConfig.load(project_root, autogenerate=True) new_project = Project(project_root=str(project_root), project_config=project_config) self.projects.append(new_project) self.save() - return new_project, new_project_config_generated + return new_project def remove_project(self, project_name: str) -> None: # find the index of the project with the desired name and remove it diff --git a/src/serena/project.py b/src/serena/project.py index 1a482f3..5e7fbe8 100644 --- a/src/serena/project.py +++ b/src/serena/project.py @@ -1,7 +1,6 @@ import logging import os from pathlib import Path -from typing import Self import pathspec @@ -53,12 +52,12 @@ class Project: return self.project_config.language @classmethod - def load(cls, project_root: str | Path, autogenerate: bool = True) -> Self: + def load(cls, project_root: str | Path, autogenerate: bool = True) -> "Project": project_root = Path(project_root).resolve() if not project_root.exists(): raise FileNotFoundError(f"Project root not found: {project_root}") project_config = ProjectConfig.load(project_root, autogenerate=autogenerate) - return cls(project_root=str(project_root), project_config=project_config) + return Project(project_root=str(project_root), project_config=project_config) def path_to_project_yml(self) -> str: return os.path.join(self.project_root, self.project_config.rel_path_to_project_yml()) diff --git a/src/serena/tools/config_tools.py b/src/serena/tools/config_tools.py index 7445b2b..db3b524 100644 --- a/src/serena/tools/config_tools.py +++ b/src/serena/tools/config_tools.py @@ -15,21 +15,15 @@ class ActivateProjectTool(Tool, ToolMarkerDoesNotRequireActiveProject): :param project: the name of a registered project to activate or a path to a project directory """ - from ..config.serena_config import ProjectConfig - - active_project, new_project_generated, new_project_config_generated = self.agent.activate_project_from_path_or_name(project) + active_project, new_project_generated = self.agent.activate_project_from_path_or_name(project) if new_project_generated: result_str = ( - f"Created and activated a new project with name {active_project.project_name} at {active_project.project_root}, language: {active_project.project_config.language.value}. " - + "You can activate this project later by name." + f"Created and activated a new project with name '{active_project.project_name}' at {active_project.project_root}, language: {active_project.project_config.language.value}. " + "You can activate this project later by name.\n" + f"The project's Serena configuration is in {active_project.path_to_project_yml()}. In particular, you may want to edit the project name and the initial prompt." ) else: - result_str = f"Activated existing project with name {active_project.project_name} at {active_project.project_root}, language: {active_project.project_config.language.value}" - if new_project_config_generated: - result_str += ( - f"\nNote: A new project configuration was autogenerated because the given path did not contain a {ProjectConfig.SERENA_DEFAULT_PROJECT_FILE} file." - + f"You can now edit the project configuration in the file {active_project.path_to_project_yml()}. In particular, you may want to edit the project name and the initial prompt." - ) + result_str = f"Activated existing project with name '{active_project.project_name}' at {active_project.project_root}, language: {active_project.project_config.language.value}" if active_project.project_config.initial_prompt: result_str += f"\nAdditional project information:\n {active_project.project_config.initial_prompt}"