fix(skill): @mention 不再收窄技能白名单

applyPerRequestSkillScope 原把 @mention 实现为收窄 AllowedSkills,
导致用户提及技能 A 后,智能体提示词要求调用未提及的技能 B 时
read_skill(B) 返回 "skill not allowed"。改为 @mention 只生成
<must_use> pin 提示,不动门控:智能体配置的全部技能始终可用,
pin 仅记录被提及且当前允许的技能(空 AllowedSkills 视为全允许,
与 Manager.isSkillAllowed 一致)。

Co-Authored-By: Claude <noreply@anthropic.com>
main
养乐多 2026-08-28 09:13:22 +08:00 committed by lyingbug
parent 12c3465b95
commit 53ba549dbb
2 changed files with 57 additions and 18 deletions

View File

@ -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 <must_use> hint.
// applyPerRequestSkillScope records the @Skill mentions for this turn as the
// pinned set that drives the <must_use> 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 <must_use> 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))

View File

@ -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)
}