Skip to content

The agent that grows with you

fix(feishu): scope default comment agent turns

KoNit-Kc7ae28da710a
diff --git a/plugins/platforms/feishu/feishu_comment.py b/plugins/platforms/feishu/feishu_comment.py
index c0a76e3d3e6..7df542a9ddc 100644
--- a/plugins/platforms/feishu/feishu_comment.py
+++ b/plugins/platforms/feishu/feishu_comment.py
@@ -575,6 +575,20 @@ async def _local_comment_prompt(client: Any, comment_id: str, reply_id: str, fro
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
+
+ if current_secret_scope() is not None:
+ return await _handle_drive_comment_event(client, data, self_open_id=self_open_id)
+ # 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.
+ from tui_gateway.launch_profile_policy import async_launch_profile_scope_if_multiplexed
+ async with async_launch_profile_scope_if_multiplexed():
+ await _handle_drive_comment_event(client, data, self_open_id=self_open_id)
+
+
+async def _handle_drive_comment_event(client: Any, data: Any, *, self_open_id: str = "") -> None:
"""Full orchestration for a drive comment event. Parse + filter (self-reply, receiver, notice_type) -> access rules -> OK reaction -> parallel fetch (doc meta +
comment) -> build timeline/prompt by is_whole -> run agent -> deliver reply -> remove OK reaction."""
logger.info("[Feishu-Comment] ========== handle_drive_comment_event START ==========")
diff --git a/tests/gateway/test_feishu_profile_scope_hops.py b/tests/gateway/test_feishu_profile_scope_hops.py
index 5991cd174bb..50d09986d9b 100644
--- a/tests/gateway/test_feishu_profile_scope_hops.py
+++ b/tests/gateway/test_feishu_profile_scope_hops.py
@@ -9,6 +9,7 @@ client drives the REAL ``_connect_websocket`` / ``_run_official_feishu_ws_client
"""
import asyncio
+from contextlib import contextmanager
import sys
import types
from pathlib import Path
@@ -81,6 +82,56 @@ def test_drive_comment_agent_turn_runs_under_the_adapter_profile_scope(routed_sc
assert seen["turn"] == ("routed", "routed-value")
+def test_drive_comment_agent_turn_binds_the_unscoped_launch_profile(tmp_path):
+ """A default-adapter SDK callback has no contextvar scope after multiplexing.
+
+ The comment entry point must install the launch profile scope before the
+ copied executor context resolves model credentials.
+ """
+ from plugins.platforms.feishu import feishu_comment as fc
+ from plugins.platforms.feishu import feishu_comment_rules as rules
+
+ launch_home = tmp_path / "launch"
+ launch_home.mkdir()
+ seen = {}
+
+ @contextmanager
+ def _launch_scope():
+ home_token = set_hermes_home_override(str(launch_home))
+ secret_token = set_secret_scope({"FEISHU_PROBE_TOKEN": "launch-value"})
+ try:
+ yield
+ finally:
+ reset_secret_scope(secret_token)
+ reset_hermes_home_override(home_token)
+
+ def _fake_agent(prompt, client, session_key=""):
+ seen["turn"] = _observe(launch_home)
+ return "NO_REPLY"
+
+ 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("tui_gateway.launch_profile_policy.launch_profile_scope_if_multiplexed", _launch_scope), \
+ patch.object(rules, "load_config", return_value=object()), \
+ patch.object(rules, "resolve_rule", return_value=rules.ResolvedCommentRule(True, "allowlist", frozenset(), "top")), \
+ patch.object(rules, "has_wiki_keys", return_value=False), \
+ patch.object(rules, "is_user_allowed", return_value=True), \
+ patch.object(fc, "query_document_meta", AsyncMock(return_value={"title": "T", "url": "u"})), \
+ patch.object(fc, "batch_query_comment", AsyncMock(return_value={"is_whole": False, "quote": ""})), \
+ patch.object(fc, "_local_comment_prompt", AsyncMock(return_value="prompt")), \
+ patch.object(fc, "_run_comment_agent", _fake_agent):
+ asyncio.run(fc.handle_drive_comment_event(object(), event, self_open_id="ou_bot"))
+ finally:
+ set_multiplex_active(False)
+
+ assert seen["turn"] == ("routed", "launch-value")
+
+
def test_ws_client_thread_and_its_loop_callbacks_carry_the_adapter_profile_scope(routed_scope, monkeypatch):
"""Lark SDK callbacks run on the WS thread and re-enter the adapter loop through
run_coroutine_threadsafe (which copies the caller's context): both must see the profile that