Repository: All-Hands-AI/OpenHands
File: openhands/server/conversation_manager/standalone_conversation_manager.py (Python)

Code before:
        default_title = get_default_conversation_title(conversation_id)
        if (
            conversation.title == default_title
        ):  # attempt to autogenerate if default title is in use
            title = await auto_generate_title(
                conversation_id, user_id, self.file_store, settings
            )
            if title and not title.isspace():
                conversation.title = title
                try:
                    # Emit a status update to the client with the new title
                    status_update_dict = {
                        'status_update': True,
                        'type': 'info',
                        'message': conversation_id,
                        'conversation_title': conversation.title,
                    }
                    await run_in_loop(
                        self.sio.emit(
                            'oh_event',
                            status_update_dict,
                            to=ROOM_KEY.format(sid=conversation_id),
                        ),
                        self._loop,  # type:ignore
                    )
                except Exception as e:
                    logger.error(f'Error emitting title update event: {e}')
            else:
                conversation.title = default_title

        await conversation_store.save_metadata(conversation)

    def _is_git_related_event(self, event) -> bool:
        """
        Determine if an event is related to git operations that could change the branch.

        Args:
            event: The event to check

        Returns:
            True if the event is git-related and could change the branch, False otherwise
        """
        # Check CmdRunAction for git commands that change branches
        if (
            hasattr(event, 'command')
            and hasattr(event, 'action')
            and event.action == ActionType.RUN
        ):
            command = event.command.lower()
            # Check if any git command that changes branches is present anywhere in the command
            # This handles compound commands like "cd workspace && git checkout feature-branch"

Review comment (suggestion):
(not a review comment)

Just a small note here: this is in a callback from an eventstream subscriber that gets run every event, I _think_ it's really any event, regardless if agent, user, environment, state changes. I always get a little concerned to return as soon as possible, if the event is not applicable.

I think in this case we're good... we identify a Cmd immediately, and git 🤔

Maybe sometime we could consider pre-filtered subscriptions, so that the filtering happens as early in the execution flow as possible.

Code after:
        llm_registry: LLMRegistry,
        event=None,
    ):
        conversation_store = await self._get_conversation_store(user_id)
        conversation = await conversation_store.get_metadata(conversation_id)
        conversation.last_updated_at = datetime.now(timezone.utc)

        # Update cost/token metrics if event has llm_metrics
        if event and hasattr(event, 'llm_metrics') and event.llm_metrics:
            metrics = event.llm_metrics

            # Update accumulated cost
            if hasattr(metrics, 'accumulated_cost'):
                conversation.accumulated_cost = metrics.accumulated_cost

            # Update token usage
            if hasattr(metrics, 'accumulated_token_usage'):
                token_usage = metrics.accumulated_token_usage
                conversation.prompt_tokens = token_usage.prompt_tokens
                conversation.completion_tokens = token_usage.completion_tokens
                conversation.total_tokens = (
                    token_usage.prompt_tokens + token_usage.completion_tokens
                )

        # Check for branch changes if this is a git-related event
        if event and self._is_git_related_event(event):
            logger.info(
                f'Git-related event detected, updating conversation branch for {conversation_id}',
                extra={
                    'session_id': conversation_id,
                    'command': getattr(event, 'command', 'unknown'),
                },
            )
            await self._update_conversation_branch(conversation)

        default_title = get_default_conversation_title(conversation_id)
        if (
            conversation.title == default_title
        ):  # attempt to autogenerate if default title is in use
            title = await auto_generate_title(
                conversation_id, user_id, self.file_store, settings, llm_registry
            )
            if title and not title.isspace():
                conversation.title = title
                try:
                    # Emit a status update to the client with the new title
                    status_update_dict = {
                        'status_update': True,
                        'type': 'info',
                        'message': conversation_id,
                        'conversation_title': conversation.title,