diff --git a/CHANGELOG.md b/CHANGELOG.md index 071cbcd7..29f774dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Status of the `main` branch. Changes prior to the next official version change w * Dashboard: - Make list of trusted hosts configurable, fixing host validation introduced in v1.5.2 allowing only default local hostnames, effectively preventing remote connections + - Decouple configuration computation from the agent's task queue by introducing events for agent config/status updates. + This allows the dashboard to display the configuration while the project provided at startup is still initialising. #1064 + - Fix empty executions queue displaying "Loading..." # v1.5.3 (2026-05-26) diff --git a/src/serena/agent.py b/src/serena/agent.py index 9542d37f..a1db940a 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -544,6 +544,7 @@ class SerenaAgent: self._project_prompt_status = ProjectPromptProvisionStatus() self._session_mode_selection_definition = modes self.version = serena_version() + self._config_changed_callbacks: list[Callable[[], None]] = [] # obtain serena configuration using the decoupled factory function self.serena_config = serena_config or SerenaConfig.from_config_file() @@ -633,7 +634,7 @@ class SerenaAgent: # create executor for starting the language server and running tools in another thread # This executor is used to achieve linear task execution - self._task_executor = TaskExecutor("SerenaAgentTaskExecutor") + self._task_executor = TaskExecutor("SerenaAgentTaskExecutor", self._task_completion_callback) # Initialize the prompt factory self.prompt_factory = SerenaPromptFactory() @@ -659,18 +660,29 @@ class SerenaAgent: self._active_tools: AvailableTools self._update_active_tools() - # start the dashboard (web frontend), registering its log handler - # should be the last thing to happen in the initialization since the dashboard - # may access various parts of the agent + # create the dashboard backend (if enabled), which will register callback. + dashboard_api: SerenaDashboardAPI | None = None if self.serena_config.web_dashboard: - self._dashboard_thread, port = SerenaDashboardAPI( + dashboard_api = SerenaDashboardAPI( get_memory_log_handler(), tool_names, agent=self, tool_usage_stats=self._tool_usage_stats, host=self.serena_config.web_dashboard_listen_address, trusted_hosts=self.serena_config.web_dashboard_trusted_hosts, - ).run_in_thread() + ) + + # propagate the initial config/state to listeners, particularly to the dashboard API. + # Note: The only ongoing task can be the LS initialisation, which does not change the config + # This is executed before starting the dashboard thread to ensure that the dashboard + # has the correct initial state before requests can come in. + self._on_config_changed() + + # start the dashboard backend thread and the dashboard frontend manager (if enabled). + # This should be the last thing to happen in the initialization since the dashboard + # may access various parts of the agent + if dashboard_api: + dashboard_thread, port = dashboard_api.run_in_thread() self._dashboard_manager = DashboardManager( port, self.serena_config.web_dashboard_listen_address, @@ -1077,6 +1089,27 @@ class SerenaAgent: """ return self._task_executor.execute_task(task, name=name, logged=logged, timeout=timeout) + def _task_completion_callback(self) -> None: + """ + Called after every task completion. Tasks potentially change the agent's state/configuration. + """ + self._on_config_changed() + + def _on_config_changed(self) -> None: + for callback in self._config_changed_callbacks: + try: + callback() + except Exception as e: + log.error(f"Error in config changed callback {callback}: {e}", exc_info=e) + + def register_config_changed_callback(self, callback: Callable[[], None]) -> None: + """ + Registers a callback to be called when the agent configuration has (potentially) changed. + + :param callback: the callback function to register + """ + self._config_changed_callbacks.append(callback) + def is_using_language_server(self) -> bool: """ :return: whether this agent uses language server-based code analysis diff --git a/src/serena/dashboard.py b/src/serena/dashboard.py index 8b15166b..7b599f9c 100644 --- a/src/serena/dashboard.py +++ b/src/serena/dashboard.py @@ -212,7 +212,12 @@ class SerenaDashboardAPI: self._news_ready = threading.Event() self._setup_routes() self._read_news = ReadNews.load() - # Fetch remote news in background on startup (non-blocking) + + # register callback for config changes + self._current_config_overview: dict[str, Any] | None = None + self._agent.register_config_changed_callback(self._on_agent_config_changed) + + # fetch remote news in background on startup (non-blocking) threading.Thread(target=self._fetch_news, daemon=True).start() @property @@ -277,8 +282,10 @@ class SerenaDashboardAPI: @self._app.route("/get_config_overview", methods=["GET"]) def get_config_overview() -> dict[str, Any]: - result = self._agent.execute_task(self._get_config_overview, logged=False) - return result.model_dump() + result = self._current_config_overview + if result is None: + raise ValueError("Config overview not yet available") + return result @self._app.route("/shutdown", methods=["PUT"]) def shutdown() -> dict[str, str]: @@ -473,7 +480,7 @@ class SerenaDashboardAPI: if self._tool_usage_stats is not None: self._tool_usage_stats.clear() - def _get_config_overview(self) -> ResponseConfigOverview: + def _compute_config_overview(self) -> ResponseConfigOverview: from serena.config.context_mode import SerenaAgentContext, SerenaAgentMode from serena.tools.tools_base import Tool @@ -601,6 +608,9 @@ class SerenaDashboardAPI: serena_version=self._agent.version, ) + def _on_agent_config_changed(self) -> None: + self._current_config_overview = self._compute_config_overview().model_dump() + def _get_available_languages(self) -> ResponseAvailableLanguages: from solidlsp.ls_config import Language diff --git a/src/serena/resources/dashboard/dashboard.css b/src/serena/resources/dashboard/dashboard.css index ad658a97..fb2d70da 100644 --- a/src/serena/resources/dashboard/dashboard.css +++ b/src/serena/resources/dashboard/dashboard.css @@ -1012,6 +1012,11 @@ code, pre, kbd, samp, font-style: italic; } +.loaded { + text-align: center; + color: var(--text-muted); +} + .error-message { color: var(--log-error); text-align: center; diff --git a/src/serena/resources/dashboard/dashboard.js b/src/serena/resources/dashboard/dashboard.js index afa71865..43987b58 100644 --- a/src/serena/resources/dashboard/dashboard.js +++ b/src/serena/resources/dashboard/dashboard.js @@ -944,6 +944,7 @@ class Dashboard { displayActiveExecutionsQueue(executions) { if (!executions || executions.length === 0) { + this.$activeExecutionQueueDisplay.html('