Skip to content

The agent that grows with you

fix(feishu): an unscoped comment from a secondary adapter fails closed

Teknium4b17231b53b2
diff --git a/plugins/platforms/feishu/adapter.py b/plugins/platforms/feishu/adapter.py
index 950694c32df..3846e7e367e 100644
--- a/plugins/platforms/feishu/adapter.py
+++ b/plugins/platforms/feishu/adapter.py
@@ -2158,10 +2158,8 @@ class FeishuAdapter(BasePlatformAdapter):
def _on_drive_comment_event(self, data: Any) -> None:
"""drive.notice.comment_add_v1 → feishu_comment.handle_drive_comment_event on the adapter loop."""
from plugins.platforms.feishu.feishu_comment import handle_drive_comment_event
- self._submit_if_ready(
- "drive comment event",
- lambda: handle_drive_comment_event(self._client, data, self_open_id=self._bot_open_id),
- )
+ self._submit_if_ready("drive comment event", lambda: handle_drive_comment_event(
+ self._client, data, self_open_id=self._bot_open_id, secondary=bool(getattr(self, "_owner_profile", None))))
def _on_meeting_invited_event(self, data: Any) -> None:
"""vc.bot.meeting_invited_v1 → feishu_meeting_invite.handle_meeting_invited_event."""
diff --git a/plugins/platforms/feishu/feishu_comment.py b/plugins/platforms/feishu/feishu_comment.py
index 7df542a9ddc..075b7ba280a 100644
--- a/plugins/platforms/feishu/feishu_comment.py
+++ b/plugins/platforms/feishu/feishu_comment.py
@@ -574,12 +574,16 @@ async def _local_comment_prompt(client: Any, comment_id: str, reply_id: str, fro
target_index=target_index, referenced_docs=await _referenced_docs_text(client, replies, file_token), **doc)
-async def handle_drive_comment_event(client: Any, data: Any, *, self_open_id: str = "") -> None:
- """Run a drive-comment event in its adapter's profile scope."""
- from agent.secret_scope import current_secret_scope
+async def handle_drive_comment_event(client: Any, data: Any, *, self_open_id: str = "", secondary: bool = False) -> None:
+ """Run a drive-comment event in its adapter's profile scope (``secondary``: the adapter belongs
+ to a multiplexed secondary profile, so it never borrows the launch scope)."""
+ from agent.secret_scope import current_secret_scope, is_multiplex_active
if current_secret_scope() is not None:
return await _handle_drive_comment_event(client, data, self_open_id=self_open_id)
+ if secondary and is_multiplex_active():
+ # Its connect-time scope was lost: fail closed instead of running on the launch credentials.
+ return logger.warning("[Feishu-Comment] Dropping unscoped comment event from a secondary profile adapter")
# SDK callbacks for the launch adapter can arrive without the connect-time
# context. Once multiplexing is active, bind the launch tenant explicitly
# before this path resolves comment rules, config, or model credentials.
diff --git a/tests/gateway/test_feishu_profile_scope_hops.py b/tests/gateway/test_feishu_profile_scope_hops.py
index deb2f29ee3d..84bd60ef835 100644
--- a/tests/gateway/test_feishu_profile_scope_hops.py
+++ b/tests/gateway/test_feishu_profile_scope_hops.py
@@ -222,3 +222,50 @@ def test_ws_client_thread_and_its_loop_callbacks_carry_the_adapter_profile_scope
asyncio.run(scenario())
assert seen == {"ws_thread": ("routed", "routed-value"), "loop_callback": ("routed", "routed-value")}
+
+
+def test_an_unscoped_comment_from_a_secondary_adapter_fails_closed(tmp_path, monkeypatch):
+ """Review on #140186: only the launch adapter's unscoped callback may borrow the launch scope.
+ A secondary's callback that lost its context must not run its comment turn on the launch
+ profile's credentials; it is refused before any rule, config or model read."""
+ from plugins.platforms.feishu import feishu_comment as fc
+
+ launch_home = tmp_path / "launch"
+ launch_home.mkdir()
+ (launch_home / ".env").write_text("FEISHU_PROBE_TOKEN=launch-env-value\n")
+ monkeypatch.setenv("HERMES_HOME", str(launch_home))
+ ran = []
+ event = SimpleNamespace(event={
+ "comment_id": "c1", "reply_id": "", "is_mentioned": True, "timestamp": "1",
+ "notice_meta": {"file_token": "docx_t", "file_type": "docx", "notice_type": "add_reply",
+ "from_user_id": {"open_id": "ou_user"}, "to_user_id": {"open_id": "ou_bot"}},
+ })
+ set_multiplex_active(True)
+ try:
+ with patch.object(fc, "_handle_drive_comment_event", AsyncMock(side_effect=lambda *a, **k: ran.append(_observe(launch_home)))):
+ asyncio.run(fc.handle_drive_comment_event(object(), event, self_open_id="ou_bot", secondary=True))
+ assert ran == []
+ asyncio.run(fc.handle_drive_comment_event(object(), event, self_open_id="ou_bot"))
+ finally:
+ set_multiplex_active(False)
+ assert ran == [("routed", "launch-env-value")]
+
+
+def test_the_adapter_tells_the_comment_handler_whether_it_is_a_secondary(monkeypatch):
+ """The Feishu adapter passes ``secondary`` from its multiplex owner profile."""
+ from plugins.platforms.feishu import adapter as fa
+ from plugins.platforms.feishu import feishu_comment as fc
+
+ calls = []
+
+ async def _handler(client, data, *, self_open_id="", secondary=False):
+ calls.append(secondary)
+ monkeypatch.setattr(fc, "handle_drive_comment_event", _handler)
+ adapter = object.__new__(fa.FeishuAdapter)
+ adapter._client, adapter._bot_open_id = object(), "ou_bot"
+ adapter._submit_if_ready = lambda label, make: asyncio.run(make())
+ adapter.set_owner_profile(None)
+ adapter._on_drive_comment_event(object())
+ adapter.set_owner_profile("b")
+ adapter._on_drive_comment_event(object())
+ assert calls == [False, True]