Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(team): team.task.created 事件回填 assignee,leader 创建任务即写到成员卡

FrancesShaw007b1ab24e61840
diff --git a/jiuwenswarm/agents/harness/team/handlers/team_monitor_handler.py b/jiuwenswarm/agents/harness/team/handlers/team_monitor_handler.py
index b99702f565..0e7d627b93 100644
--- a/jiuwenswarm/agents/harness/team/handlers/team_monitor_handler.py
+++ b/jiuwenswarm/agents/harness/team/handlers/team_monitor_handler.py
@@ -216,17 +216,22 @@ class TeamMonitorHandler(BaseMonitorHandler):
})
return base
- async def _lookup_task_body(self, task_id: str) -> tuple[str, str] | None:
- """Re-query the task's title/content via the monitor's public API.
+ async def _lookup_task_body(self, task_id: str) -> tuple[str | None, str | None, str | None] | None:
+ """Re-query the task's title/content/assignee via the monitor's public API.
Args:
task_id: The DB task id carried by the monitor event.
Returns:
- ``(title, content)`` when the task exists, else ``None``. On any
- exception (monitor None / query error) logs a warning and returns
- ``None`` so the caller can still emit the event with ``task_id`` +
- ``status`` (no body) — never blocks the event stream.
+ ``(title, content, assignee)`` when the task exists, else ``None``.
+ On any exception (monitor None / query error) logs a warning and
+ returns ``None`` so the caller can still emit the event with
+ ``task_id`` + ``status`` (no body) — never blocks the event stream.
+ ``assignee`` is included so ``_handle_task`` can backfill the
+ assignee on ``TASK_CREATED`` / ``TASK_UPDATED`` (whose monitor
+ events carry no ``member_name``), letting the frontend attach a
+ freshly-created task to its assignee's card without waiting for a
+ later ``TASK_CLAIMED`` event.
"""
if self._monitor is None or not task_id:
return None
@@ -241,7 +246,7 @@ class TeamMonitorHandler(BaseMonitorHandler):
return None
if task is None:
return None
- return task.title, task.content
+ return task.title, task.content, getattr(task, "assignee", None)
async def _handle_task(self, base: dict[str, Any], event: MonitorEvent) -> dict[str, Any]:
"""Converge every task event into the frontend-ready task shape.
@@ -279,6 +284,14 @@ class TeamMonitorHandler(BaseMonitorHandler):
content_field = _task_text_field("content", body[1])
base.update(title_field)
base.update(content_field)
+ # TASK_CREATED/TASK_UPDATED 的 monitor 事件不带 member_name(见
+ # events.py:TaskCreatedEvent 不设 member_name),故 _convert_event_to_dict
+ # 不会写入 member_id。此处从 DB 补认领人进 assignee 字段,使前端在
+ # leader 创建任务当下即把任务挂到对应成员卡,无需等 TASK_CLAIMED。
+ # 仅当事件本身未携带 member_id 时回填,避免覆盖 claim 等事件自带的权威认领人。
+ assignee = body[2]
+ if assignee and "member_id" not in base:
+ base["assignee"] = assignee
return base
async def _handle_message(self, base: dict[str, Any], event: MonitorEvent) -> dict[str, Any]:
diff --git a/tests/unit_tests/agentserver/test_monitor_handler.py b/tests/unit_tests/agentserver/test_monitor_handler.py
index 812230582c..e7b56d8c33 100644
--- a/tests/unit_tests/agentserver/test_monitor_handler.py
+++ b/tests/unit_tests/agentserver/test_monitor_handler.py
@@ -514,6 +514,54 @@ async def test_handle_task_created_supplements_title_and_content_from_db() -> No
assert task_dao.get_task_call_count == 1
+async def test_handle_task_created_backfills_assignee_from_db() -> None:
+ """TASK_CREATED carries no member_name; assignee is backfilled from the DB.
+
+ Lets the frontend attach a freshly-created task to its assignee's card
+ without waiting for a later TASK_CLAIMED event.
+ """
+ task_dao = _FakeTaskDao(
+ _FakeTask(task_id="task-1", title="research", content="do", assignee="worker-1")
+ )
+ handler = _make_handler(task_dao)
+ base = {"type": "team.task.created", "team_id": "team-1"}
+ event = MonitorEvent(
+ event_type=MonitorEventType.TASK_CREATED,
+ team_name="team-1",
+ timestamp=123,
+ task_id="task-1",
+ status=None,
+ )
+
+ result = await handler._handle_task(base, event)
+
+ assert result["assignee"] == "worker-1"
+ assert "member_id" not in result
+ assert task_dao.get_task_call_count == 1
+
+
+async def test_handle_task_created_no_assignee_when_db_task_unassigned() -> None:
+ """An unassigned DB task must not synthesize an assignee field."""
+ task_dao = _FakeTaskDao(
+ _FakeTask(task_id="task-1", title="research", content="do", assignee=None)
+ )
+ handler = _make_handler(task_dao)
+ base = {"type": "team.task.created", "team_id": "team-1"}
+ event = MonitorEvent(
+ event_type=MonitorEventType.TASK_CREATED,
+ team_name="team-1",
+ timestamp=123,
+ task_id="task-1",
+ status=None,
+ )
+
+ result = await handler._handle_task(base, event)
+
+ assert "assignee" not in result
+
+
@pytest.mark.anyio
async def test_handle_task_updated_supplements_body_from_db() -> None:
"""TASK_UPDATED also triggers the DB body lookup."""