refactor: update risk levels in tool definitions and validation logic to remove sensitive level

This commit is contained in:
mlogclub
2026-07-26 22:32:16 +08:00
parent c021c5568c
commit 2de50a2f3a
5 changed files with 11 additions and 9 deletions
+1 -1
Submodule docs updated: ab3cb38eda...50814016a3
@@ -65,7 +65,7 @@ func (e *fakeMCPToolExecutor) Execute(_ context.Context, toolCode string, argume
e.toolCode = toolCode e.toolCode = toolCode
e.arguments = arguments e.arguments = arguments
e.policy = policy e.policy = policy
return aitooling.Definition{Code: toolCode, RiskLevel: aitooling.RiskLevelSensitive}, e.result, e.err return aitooling.Definition{Code: toolCode, RiskLevel: aitooling.RiskLevelRead}, e.result, e.err
} }
var _ model.ToolCallingChatModel = (*scriptedToolCallingModel)(nil) var _ model.ToolCallingChatModel = (*scriptedToolCallingModel)(nil)
+2 -3
View File
@@ -11,9 +11,8 @@ import (
) )
const ( const (
RiskLevelRead = "read" RiskLevelRead = "read"
RiskLevelWrite = "write" RiskLevelWrite = "write"
RiskLevelSensitive = "sensitive"
) )
// Definition is the normalized, engine-independent description of a tool. // Definition is the normalized, engine-independent description of a tool.
+2 -2
View File
@@ -248,7 +248,7 @@ func (s *aIAgentService) validatePublishableAgent(db *gorm.DB, agent *models.AIA
return errorsx.InvalidParam("ai agent direct tool definition is unavailable") return errorsx.InvalidParam("ai agent direct tool definition is unavailable")
} }
if definition.RequireConfirmation { if definition.RequireConfirmation {
return errorsx.InvalidParam("ai agent sensitive direct tools must be executed through a confirmed playbook") return errorsx.InvalidParam("ai agent direct tool requires confirmation and cannot be executed directly")
} }
} }
return nil return nil
@@ -456,7 +456,7 @@ func (s *aIAgentService) normalizeToolPolicy(raw string) (string, error) {
if level == "" { if level == "" {
continue continue
} }
if level != "read" && level != "write" && level != "sensitive" { if level != "read" && level != "write" {
return "", errorsx.InvalidParam("ai agent tool policy contains an invalid risk level") return "", errorsx.InvalidParam("ai agent tool policy contains an invalid risk level")
} }
if _, exists := seen[level]; exists { if _, exists := seen[level]; exists {
@@ -185,13 +185,16 @@ func TestAIAgentServiceCreatesWorkflowDraftForHybrid(t *testing.T) {
} }
func TestAIAgentServiceNormalizesToolPolicy(t *testing.T) { func TestAIAgentServiceNormalizesToolPolicy(t *testing.T) {
policy, err := AIAgentService.normalizeToolPolicy(`{"maxTotalCalls":2,"maxArgumentBytes":1024,"allowedRiskLevels":["READ","read","sensitive"]}`) policy, err := AIAgentService.normalizeToolPolicy(`{"maxTotalCalls":2,"maxArgumentBytes":1024,"allowedRiskLevels":["READ","read","write"]}`)
if err != nil { if err != nil {
t.Fatalf("normalizeToolPolicy: %v", err) t.Fatalf("normalizeToolPolicy: %v", err)
} }
if !strings.Contains(policy, `"maxTotalCalls":2`) || !strings.Contains(policy, `"allowedRiskLevels":["read","sensitive"]`) { if !strings.Contains(policy, `"maxTotalCalls":2`) || !strings.Contains(policy, `"allowedRiskLevels":["read","write"]`) {
t.Fatalf("unexpected normalized policy: %s", policy) t.Fatalf("unexpected normalized policy: %s", policy)
} }
if _, err := AIAgentService.normalizeToolPolicy(`{"allowedRiskLevels":["sensitive"]}`); err == nil {
t.Fatal("expected removed sensitive risk level to be rejected")
}
if _, err := AIAgentService.normalizeToolPolicy(`{"allowedRiskLevels":["admin"]}`); err == nil { if _, err := AIAgentService.normalizeToolPolicy(`{"allowedRiskLevels":["admin"]}`); err == nil {
t.Fatal("expected invalid risk level error") t.Fatal("expected invalid risk level error")
} }