Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Validate agent tools before confirmation

Daniel4cbc84600623
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index ddb69213e7..0b01d897a5 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -993,7 +993,8 @@ func AgentChat(ctx context.Context, client *openai.Client, model, imageCapabilit
Arguments: args,
})
- if needsConfirm(tc.Function.Name, action, alwaysAllow) {
+ _, _, toolInputErr := validateToolCallInput(ctx, tc.Function.Name, args)
+ if toolInputErr == nil && needsConfirm(tc.Function.Name, action, alwaysAllow) {
confirmID := fmt.Sprintf("%s_%s_%d", turn.TurnID, tc.ID, i)
ch2 := make(chan confirmResult, 1)
confirmChannelsMu.Lock()
@@ -1084,7 +1085,7 @@ func AgentChat(ctx context.Context, client *openai.Client, model, imageCapabilit
default:
}
- if !snapshotCreated && needsLocalSnapshot(tc.Function.Name, action) {
+ if toolInputErr == nil && !snapshotCreated && needsLocalSnapshot(tc.Function.Name, action) {
id, err := kernelModel.IndexRepo("AI agent auto snapshot")
if err != nil {
logging.LogErrorf("agent auto snapshot failed: %s", err)
@@ -1140,7 +1141,10 @@ func AgentChat(ctx context.Context, client *openai.Client, model, imageCapabilit
var modelAttachments []mcptools.ModelAttachment
isErr := false
executionUnknown := false
- if tc.Function.Name == "question" {
+ if toolInputErr != nil {
+ resultStr = toolInputErr.Error()
+ isErr = true
+ } else if tc.Function.Name == "question" {
resultStr = handleQuestion(ctx, tc.Function.Arguments, ch, 5*time.Minute)
} else if tc.Function.Name == "frontend" {
resultStr, executionUnknown = handleFrontendTool(ctx, tc, ch, confirmTimeout)
diff --git a/kernel/agent/tools.go b/kernel/agent/tools.go
index 23196b39c7..d95a8874c1 100644
--- a/kernel/agent/tools.go
+++ b/kernel/agent/tools.go
@@ -19,6 +19,7 @@ package agent
import (
"context"
"encoding/json"
+ "fmt"
"maps"
"strings"
@@ -50,22 +51,30 @@ type executedToolResult struct {
ExecutionUnknown bool
}
-// executeTool 执行单次工具调用。
-func executeTool(ctx context.Context, tc openai.ToolCall, sessionID string) executedToolResult {
- t, validator := tools.LookupToolWithValidator(tc.Function.Name)
+// validateToolCallInput 在确认和快照之前校验工具调用,避免无效调用被误判为写操作。
+func validateToolCallInput(ctx context.Context, toolName string, args map[string]any) (*tools.Tool, *tools.ToolValidator, error) {
+ t, validator := tools.LookupToolWithValidator(toolName)
if t == nil {
- return executedToolResult{Text: "unknown tool: " + tc.Function.Name, IsError: true}
+ return nil, nil, fmt.Errorf("unknown tool: %s", toolName)
}
if t.ContextHandler == nil && t.Handler == nil {
- return executedToolResult{Text: "tool handler unavailable: " + tc.Function.Name, IsError: true}
+ return nil, nil, fmt.Errorf("tool handler unavailable: %s", toolName)
}
if ctx.Err() != nil {
- return executedToolResult{Text: "tool execution was cancelled before it started", IsError: true}
+ return nil, nil, fmt.Errorf("tool execution was cancelled before it started")
}
+ if err := validator.ValidateInputContext(ctx, args); err != nil {
+ return nil, nil, fmt.Errorf("invalid tool arguments: %w", err)
+ }
+ return t, validator, nil
+}
+// executeTool 执行单次工具调用。
+func executeTool(ctx context.Context, tc openai.ToolCall, sessionID string) executedToolResult {
args := parseToolArgs(tc.Function.Arguments)
- if err := validator.ValidateInputContext(ctx, args); err != nil {
- return executedToolResult{Text: "invalid tool arguments: " + err.Error(), IsError: true}
+ t, validator, err := validateToolCallInput(ctx, tc.Function.Name, args)
+ if err != nil {
+ return executedToolResult{Text: err.Error(), IsError: true}
}
// _sessionID 和 _toolCallID 是原生工具专用的内部字段,用于关联会话状态和实现幂等操作。
// 仅注入给原生工具;MCP/插件工具的参数会原样转发给外部服务端,
diff --git a/kernel/agent/tools_test.go b/kernel/agent/tools_test.go
index 086b30e919..076164abfa 100644
--- a/kernel/agent/tools_test.go
+++ b/kernel/agent/tools_test.go
@@ -166,6 +166,17 @@ func TestExecuteToolPreservesModelAttachments(t *testing.T) {
}
}
+func TestValidateToolCallInputRejectsMissingActionBeforeConfirmation(t *testing.T) {
+ args := map[string]any{"id": "20260707184942-prjqwqo"}
+ if _, _, err := validateToolCallInput(t.Context(), "outline", args); err == nil {
+ t.Fatal("outline without its required action must fail validation before confirmation")
+ }
+ args["action"] = "get"
+ if _, _, err := validateToolCallInput(t.Context(), "outline", args); err != nil {
+ t.Fatalf("valid outline arguments were rejected: %s", err)
+ }
+}
+
func TestNeedsConfirmScopesReadOnlyActionsByToolSource(t *testing.T) {
const externalWrite = "test_external_write"
const externalRead = "test_external_read"