[PR #30861] fix: dynamically set created_by_role and created_by in BaseAgentRunner #32989

Open
opened 2026-02-21 20:52:27 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/30861

State: open
Merged: No


fix: dynamically set created_by_role and created_by in BaseAgentRunner based on message source

fix #30860

Description

Fixes a bug where created_by_role and created_by were hardcoded in BaseAgentRunner.create_agent_thought(), causing all agent thought records to be incorrectly attributed to account users even when messages originated from end users.

Problem

The create_agent_thought() method in BaseAgentRunner was hardcoding:

  • created_by_role = CreatorUserRole.ACCOUNT
  • created_by = self.user_id

This meant that all MessageAgentThought records were incorrectly marked as account-created, regardless of whether the message came from an end user or account user.

Solution

  • Added _get_creator_info() helper method that inspects self.message to determine the correct creator information
  • Updated create_agent_thought() to use dynamic creator information instead of hardcoded values
  • Logic flow:
    1. If self.message.from_end_user_id is present → set created_by_role to END_USER and created_by to from_end_user_id
    2. Else if self.message.from_account_id is present → set created_by_role to ACCOUNT and created_by to from_account_id
    3. Otherwise → fall back to ACCOUNT role and self.user_id
**Original Pull Request:** https://github.com/langgenius/dify/pull/30861 **State:** open **Merged:** No --- fix: dynamically set created_by_role and created_by in BaseAgentRunner based on message source fix #30860 ### Description Fixes a bug where `created_by_role` and `created_by` were hardcoded in `BaseAgentRunner.create_agent_thought()`, causing all agent thought records to be incorrectly attributed to account users even when messages originated from end users. ### Problem The `create_agent_thought()` method in `BaseAgentRunner` was hardcoding: - `created_by_role = CreatorUserRole.ACCOUNT` - `created_by = self.user_id` This meant that all `MessageAgentThought` records were incorrectly marked as account-created, regardless of whether the message came from an end user or account user. ### Solution - Added `_get_creator_info()` helper method that inspects `self.message` to determine the correct creator information - Updated `create_agent_thought()` to use dynamic creator information instead of hardcoded values - Logic flow: 1. If `self.message.from_end_user_id` is present → set `created_by_role` to `END_USER` and `created_by` to `from_end_user_id` 2. Else if `self.message.from_account_id` is present → set `created_by_role` to `ACCOUNT` and `created_by` to `from_account_id` 3. Otherwise → fall back to `ACCOUNT` role and `self.user_id`
yindo added the pull-request label 2026-02-21 20:52:27 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32989