From 53ba549dbbd43299ce1297a6de542c3f3bc04b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=BB=E4=B9=90=E5=A4=9A?= <865730675@qq.com> Date: Fri, 28 Aug 2026 09:13:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(skill):=20@mention=20=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E6=94=B6=E7=AA=84=E6=8A=80=E8=83=BD=E7=99=BD=E5=90=8D=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit applyPerRequestSkillScope 原把 @mention 实现为收窄 AllowedSkills, 导致用户提及技能 A 后,智能体提示词要求调用未提及的技能 B 时 read_skill(B) 返回 "skill not allowed"。改为 @mention 只生成 pin 提示,不动门控:智能体配置的全部技能始终可用, pin 仅记录被提及且当前允许的技能(空 AllowedSkills 视为全允许, 与 Manager.isSkillAllowed 一致)。 Co-Authored-By: Claude --- .../application/service/session_agent_qa.go | 52 ++++++++++++++----- .../service/session_agent_qa_scope_test.go | 23 ++++++-- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/internal/application/service/session_agent_qa.go b/internal/application/service/session_agent_qa.go index fb09f4c8..9fd0cea8 100644 --- a/internal/application/service/session_agent_qa.go +++ b/internal/application/service/session_agent_qa.go @@ -436,8 +436,12 @@ func mergeResolvedTagKnowledgeIDs( return uniqueNonEmptyStrings(merged) } -// applyPerRequestSkillScope narrows the agent's skill whitelist to the @Skill -// mentions for this turn and records the pinned set for the hint. +// applyPerRequestSkillScope records the @Skill mentions for this turn as the +// pinned set that drives the hint. It deliberately does NOT narrow +// the allow-gate: an agent whose prompt requires a skill the user did not +// @mention must still be able to read and execute it. Mentioning a skill only +// prioritizes it, it never revokes access to the agent's configured set. +// // It is a no-op when no skills were mentioned or skills are disabled. func applyPerRequestSkillScope( ctx context.Context, @@ -455,18 +459,11 @@ func applyPerRequestSkillScope( if !agentConfig.SkillsEnabled { return } - switch skillsMode { - case "selected": - agentConfig.AllowedSkills = intersectPreservingRequestOrder(requested, agentConfig.AllowedSkills) - if len(agentConfig.AllowedSkills) == 0 { - agentConfig.SkillsEnabled = false - } - case "all": - agentConfig.AllowedSkills = dedupPreservingOrder(requested) - } - if agentConfig.SkillsEnabled && len(agentConfig.AllowedSkills) > 0 { - agentConfig.PinnedSkillNames = intersectPreservingRequestOrder(requested, agentConfig.AllowedSkills) - } + // PinnedSkillNames carries only mentioned skills that are currently + // allowed, so the hint never directs the model at a skill it + // cannot load. An empty AllowedSkills means all skills are allowed, + // matching Manager.isSkillAllowed, so every mention is pinned in that case. + agentConfig.PinnedSkillNames = pinPreservingRequestOrder(requested, agentConfig.AllowedSkills) logger.Infof(ctx, "Applied per-request @skill scope: requested=%v effective=%v pinned=%v", requested, agentConfig.AllowedSkills, agentConfig.PinnedSkillNames) } @@ -554,6 +551,33 @@ func intersectPreservingRequestOrder(requested []string, allowed []string) []str return result } +// pinPreservingRequestOrder returns the requested skills that are allowed, +// preserving request order. Unlike intersectPreservingRequestOrder, an empty +// allowed list is treated as "all skills allowed" (matching +// Manager.isSkillAllowed), so every requested skill is pinned. +func pinPreservingRequestOrder(requested []string, allowed []string) []string { + allowedAll := len(allowed) == 0 + allowedSet := make(map[string]bool, len(allowed)) + for _, value := range allowed { + if value != "" { + allowedSet[value] = true + } + } + result := make([]string, 0, len(requested)) + seen := make(map[string]bool, len(requested)) + for _, value := range requested { + if value == "" || seen[value] { + continue + } + if !allowedAll && !allowedSet[value] { + continue + } + seen[value] = true + result = append(result, value) + } + return result +} + func dedupPreservingOrder(values []string) []string { result := make([]string, 0, len(values)) seen := make(map[string]bool, len(values)) diff --git a/internal/application/service/session_agent_qa_scope_test.go b/internal/application/service/session_agent_qa_scope_test.go index 82d07896..af23df07 100644 --- a/internal/application/service/session_agent_qa_scope_test.go +++ b/internal/application/service/session_agent_qa_scope_test.go @@ -78,18 +78,33 @@ func TestApplyPerRequestMCPScope_NoneIgnoresMentionAndDoesNotPin(t *testing.T) { assert.Empty(t, cfg.PinnedMCPServiceIDs) } -func TestApplyPerRequestSkillScope_SelectedEmptyIntersectionDisables(t *testing.T) { +func TestApplyPerRequestSkillScope_SelectedPinsMentionedAndKeepsAllowed(t *testing.T) { + cfg := &types.AgentConfig{SkillsEnabled: true, AllowedSkills: []string{"a", "b"}} + applyPerRequestSkillScope(context.Background(), cfg, "selected", []string{"a"}) + assert.True(t, cfg.SkillsEnabled) + // Allow-gate is not narrowed: a prompt-mandated skill the user did not + // @mention (b) must remain callable. + assert.Equal(t, []string{"a", "b"}, cfg.AllowedSkills) + assert.Equal(t, []string{"a"}, cfg.PinnedSkillNames) +} + +func TestApplyPerRequestSkillScope_SelectedMentionOutsideAllowedIsNotPinned(t *testing.T) { cfg := &types.AgentConfig{SkillsEnabled: true, AllowedSkills: []string{"a", "b"}} applyPerRequestSkillScope(context.Background(), cfg, "selected", []string{"c"}) - assert.False(t, cfg.SkillsEnabled) + // Skills stay enabled (no narrowing-to-empty disable); the out-of-scope + // mention is simply not pinned. + assert.True(t, cfg.SkillsEnabled) + assert.Equal(t, []string{"a", "b"}, cfg.AllowedSkills) assert.Empty(t, cfg.PinnedSkillNames) } -func TestApplyPerRequestSkillScope_AllPinsMentioned(t *testing.T) { +func TestApplyPerRequestSkillScope_AllPinsMentionedWithoutNarrowingGate(t *testing.T) { cfg := &types.AgentConfig{SkillsEnabled: true} applyPerRequestSkillScope(context.Background(), cfg, "all", []string{"analysis", "analysis"}) assert.True(t, cfg.SkillsEnabled) - assert.Equal(t, []string{"analysis"}, cfg.AllowedSkills) + // "all" mode keeps AllowedSkills empty (= all allowed); it does not narrow + // to only the mentioned set, so other skills the agent needs stay callable. + assert.Empty(t, cfg.AllowedSkills) assert.Equal(t, []string{"analysis"}, cfg.PinnedSkillNames) }