From 761f8577bec3dad9d17c754c1812a0143996811e Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Thu, 18 Jun 2026 10:40:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(tools):=20=E4=BF=AE=E6=AD=A3=20scope=5Fkey?= =?UTF-8?q?=20=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91=E5=8F=8A=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E8=BF=90=E8=A1=8C=E6=97=B6=20topic=5Fid=20=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整 scope_key_from_context 根据 nesting_depth 区分主代理和子代理的 key 策略 - 主代理优先使用 topic_id,子代理使用 session_id 避免污染父代理 todo - 任务运行时将 parent_topic_id 传入 topic_id,修正为正确的引用 - 完善函数注释,增强代码可读性和维护性 --- src/tools/task/runtime.rs | 2 +- src/tools/todo_write.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/tools/task/runtime.rs b/src/tools/task/runtime.rs index e111dac..8e3192f 100644 --- a/src/tools/task/runtime.rs +++ b/src/tools/task/runtime.rs @@ -342,7 +342,7 @@ impl DefaultSubAgentRuntime { sender_id: None, chat_id: Some(session.parent_chat_id.clone()), session_id: Some(session.session_id.clone()), - topic_id: None, + topic_id: session.parent_topic_id.clone(), message_id: None, message_seq: None, subagent_description: Some(session.description.clone()), diff --git a/src/tools/todo_write.rs b/src/tools/todo_write.rs index 1656be6..6195e48 100644 --- a/src/tools/todo_write.rs +++ b/src/tools/todo_write.rs @@ -328,11 +328,17 @@ impl Tool for TodoWriteTool { // ── 辅助函数 ────────────────────────────────────────────── -/// 计算 scope_key:优先 topic_id,否则 session_id +/// 计算 scope_key: +/// - 主代理 (nesting_depth == 0):优先 topic_id,否则 session_id +/// - 子/孙代理 (nesting_depth > 0):使用 session_id 隔离,避免污染父代理 todo pub(crate) fn scope_key_from_context(context: &ToolContext) -> Option { - let tid = context.topic_id.as_deref().filter(|t| !t.is_empty()); - let sid = context.session_id.as_deref().filter(|s| !s.is_empty()); - tid.or(sid).map(str::to_string) + if context.nesting_depth > 0 { + context.session_id.clone().filter(|s| !s.is_empty()) + } else { + let tid = context.topic_id.as_deref().filter(|t| !t.is_empty()); + let sid = context.session_id.as_deref().filter(|s| !s.is_empty()); + tid.or(sid).map(str::to_string) + } } /// 校验状态转换合法性