From 6d0f09d75f06f068bf3ef63bd61c3f0a976c2754 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Sun, 10 May 2026 21:41:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(agent):=20=E5=8A=A0=E5=9B=BA=20MCP=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E4=BA=BA=E5=AE=A1=E6=89=B9=E7=9A=84=E8=B7=A8?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E4=B8=8E=E5=B9=B6=E5=8F=91=E8=AF=AD=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 跨实例 Resolve 通过 Redis reply channel 接收 owning 实例的 ack (ok / not_found / tenant_mismatch / user_mismatch / already_resolved), HTTP 不再静默 200,超时退化为 NotFound。 - PendingRequest 增加 UserID,Resolve 校验会话所有者,避免同租户 其他用户越权批准/篡改参数。 - 新增 ErrAlreadyResolved,timer/ctx 命中后再次 Resolve 返回 400 而 不是静默成功;waiter.deliver 暴露胜出语义。 - NeedsApproval 默认 fail-close(DB 故障要求审批),可经 WEKNORA_AGENT_TOOL_APPROVAL_FAIL_OPEN=true 回退。 - Pub/Sub 增加 OriginID 自过滤、退避重连、namespace 后缀,避免自回 环噪声与多部署串扰。 - 工具执行超时常量统一通过 ToolExecContext.ExecTimeout 注入, mcp_tool.go 不再硬编码 60s。 - handler 修正 path 二次解码、错误用 errors.Is、modified_args=null 的 静默清空、ListMCPToolApprovals 的 404/500 误判。 - repository 用 clause.OnConflict 做原子 upsert,消除并发 500。 - 增加 NotFound / TenantMismatch / UserMismatch / AlreadyResolved / Race 等用例。 --- internal/agent/act.go | 3 + internal/agent/approval/gate.go | 271 ++++++++++++++---- internal/agent/approval/gate_test.go | 120 +++++++- internal/agent/tools/exec_context.go | 7 + internal/agent/tools/mcp_tool.go | 9 +- .../mcp_tool_approval_repository.go | 43 +-- internal/handler/mcp_service.go | 51 ++-- 7 files changed, 416 insertions(+), 88 deletions(-) diff --git a/internal/agent/act.go b/internal/agent/act.go index 810b6724..a938f98e 100644 --- a/internal/agent/act.go +++ b/internal/agent/act.go @@ -392,14 +392,17 @@ func (e *AgentEngine) runToolCall( }, }) + userID, _ := types.UserIDFromContext(ctx) toolExecCtx := agenttools.WithToolExecContext(toolCtx, &agenttools.ToolExecContext{ SessionID: sessionID, AssistantMessageID: assistantMessageID, EventBus: e.eventBus, ToolCallID: tc.ID, + UserID: userID, // ApprovalCtx keeps the round-level ctx without the per-tool 60s timeout, // so MCP tool human-approval (issue #1173) can legitimately block longer. ApprovalCtx: toolCtx, + ExecTimeout: defaultToolExecTimeout, }) execCtx, toolCancel := context.WithTimeout(toolExecCtx, defaultToolExecTimeout) diff --git a/internal/agent/approval/gate.go b/internal/agent/approval/gate.go index ba8a482b..ae2ee931 100644 --- a/internal/agent/approval/gate.go +++ b/internal/agent/approval/gate.go @@ -6,6 +6,8 @@ import ( "encoding/json" "errors" "fmt" + "os" + "strings" "sync" "time" @@ -16,20 +18,47 @@ import ( "github.com/redis/go-redis/v9" ) -// pubsubChannel is the Redis channel used to fan-out Resolve calls across -// backend replicas (issue #1173 cross-instance support). -const pubsubChannel = "weknora:mcp_approval:resolve" +// pubsubChannelBase is the Redis channel prefix used to fan-out Resolve calls +// across backend replicas (issue #1173 cross-instance support). The actual +// channel is suffixed with WEKNORA_REDIS_NAMESPACE (when set) so multiple +// deployments sharing the same Redis don't cross-talk. +const pubsubChannelBase = "weknora:mcp_approval:resolve" + +// instanceID is a process-unique id used to ignore self-published pubsub +// messages (avoids "pending not found" log noise on the publisher side). +var instanceID = uuid.New().String() // resolveMessage is the JSON payload published when one instance receives a // Resolve API call but the pending wait may live on another instance. type resolveMessage struct { TenantID uint64 `json:"tenant_id"` + UserID string `json:"user_id,omitempty"` PendingID string `json:"pending_id"` Approved bool `json:"approved"` ModifiedArgs json.RawMessage `json:"modified_args,omitempty"` Reason string `json:"reason,omitempty"` TimedOut bool `json:"timed_out,omitempty"` Canceled bool `json:"canceled,omitempty"` + // ReplyChannel, when non-empty, asks the owning instance to publish a + // resolveAck so the caller can know if delivery actually happened. + ReplyChannel string `json:"reply_channel,omitempty"` + OriginID string `json:"origin_id,omitempty"` +} + +// resolveAck is published by the owning instance back to the caller's +// per-pending reply channel so HTTP semantics stay accurate cross-instance. +type resolveAck struct { + PendingID string `json:"pending_id"` + Status string `json:"status"` // ok | not_found | tenant_mismatch | user_mismatch | already_resolved + OriginID string `json:"origin_id,omitempty"` +} + +// pubsubChannel returns the namespaced pubsub channel name. +func pubsubChannel() string { + if ns := strings.TrimSpace(os.Getenv("WEKNORA_REDIS_NAMESPACE")); ns != "" { + return pubsubChannelBase + ":" + ns + } + return pubsubChannelBase } // Checker answers whether a concrete MCP tool requires human approval before execution. @@ -49,6 +78,7 @@ type Decision struct { // PendingRequest carries everything needed to block and notify the UI. type PendingRequest struct { TenantID uint64 + UserID string // owner of the session that initiated the call (used for Resolve authorization); empty disables user check SessionID string AssistantMessageID string RequestID string @@ -78,29 +108,39 @@ var _ MCPApproval = (*Gate)(nil) // (issue #1173 cross-instance support). Without redis, the gate degrades to // single-process behavior (deployments must use sticky sessions). type Gate struct { - mu sync.Mutex - pending map[string]*waiter - checker Checker - timeout time.Duration - rdb *redis.Client // optional; nil disables cross-instance fan-out + mu sync.Mutex + pending map[string]*waiter + checker Checker + timeout time.Duration + rdb *redis.Client // optional; nil disables cross-instance fan-out + failClose bool // when true, NeedsApproval errors block (require approval) instead of skip } type waiter struct { ch chan Decision tenantID uint64 + userID string // empty means "skip user check" once sync.Once + resolved bool // set true once a decision was delivered (by user, timer, or ctx) } -func (w *waiter) deliver(d Decision) { +// deliver returns true when this call won the race and actually delivered the +// decision; false when a previous deliver already happened. Callers can use +// the return value to map to ErrAlreadyResolved for HTTP feedback. +func (w *waiter) deliver(d Decision) bool { if w == nil { - return + return false } + delivered := false w.once.Do(func() { select { case w.ch <- d: + w.resolved = true + delivered = true default: } }) + return delivered } var ( @@ -108,6 +148,13 @@ var ( ErrPendingNotFound = errors.New("tool approval pending not found") // ErrTenantMismatch is returned when Resolve tenant does not match the pending request. ErrTenantMismatch = errors.New("tenant mismatch for tool approval") + // ErrAlreadyResolved is returned when Resolve loses the race against a + // timeout / cancellation: the waiter is still in the map but its decision + // channel was already consumed by the timer/ctx branch. + ErrAlreadyResolved = errors.New("tool approval already resolved") + // ErrUserMismatch is returned when Resolve is invoked by a user that does + // not own the originating session. + ErrUserMismatch = errors.New("user mismatch for tool approval") ) // NewGate builds a gate. checker may be nil (disables gating). cfg may be nil @@ -117,11 +164,15 @@ func NewGate(cfg *config.Config, checker Checker, rdb *redis.Client) *Gate { if cfg != nil && cfg.Agent != nil && cfg.Agent.ToolApprovalTimeoutSeconds > 0 { timeout = time.Duration(cfg.Agent.ToolApprovalTimeoutSeconds) * time.Second } + // Default fail-close: if the checker errors, require approval (safer for a + // HITL feature). Set WEKNORA_AGENT_TOOL_APPROVAL_FAIL_OPEN=true to revert. + failClose := !strings.EqualFold(strings.TrimSpace(os.Getenv("WEKNORA_AGENT_TOOL_APPROVAL_FAIL_OPEN")), "true") g := &Gate{ - pending: make(map[string]*waiter), - checker: checker, - timeout: timeout, - rdb: rdb, + pending: make(map[string]*waiter), + checker: checker, + timeout: timeout, + rdb: rdb, + failClose: failClose, } if rdb != nil { go g.runSubscriber() @@ -133,28 +184,76 @@ func NewGate(cfg *config.Config, checker Checker, rdb *redis.Client) *Gate { // decisions to local waiters. Runs for the lifetime of the process. func (g *Gate) runSubscriber() { ctx := context.Background() + channel := pubsubChannel() + backoff := time.Second + const maxBackoff = 30 * time.Second for { - sub := g.rdb.Subscribe(ctx, pubsubChannel) + sub := g.rdb.Subscribe(ctx, channel) ch := sub.Channel() + // Reset backoff once we have an active subscription. + backoff = time.Second for msg := range ch { var m resolveMessage if err := json.Unmarshal([]byte(msg.Payload), &m); err != nil { logger.GetLogger(ctx).Warnf("mcp approval pubsub: bad payload: %v", err) continue } - if err := g.deliverLocal(m.TenantID, m.PendingID, Decision{ + // Skip messages we published ourselves; they would always miss + // locally and produce noisy ErrPendingNotFound. + if m.OriginID == instanceID { + continue + } + err := g.deliverLocal(m.TenantID, m.UserID, m.PendingID, Decision{ Approved: m.Approved, ModifiedArgs: m.ModifiedArgs, Reason: m.Reason, TimedOut: m.TimedOut, ContextCanceled: m.Canceled, - }); err != nil && !errors.Is(err, ErrPendingNotFound) { + }) + // Reply to the originating instance so it can return accurate HTTP + // status codes. Only the owning instance (or one that detects a + // real conflict) replies; other replicas stay silent on NotFound. + if m.ReplyChannel != "" { + status := "" + switch { + case err == nil: + status = "ok" + case errors.Is(err, ErrTenantMismatch): + status = "tenant_mismatch" + case errors.Is(err, ErrUserMismatch): + status = "user_mismatch" + case errors.Is(err, ErrAlreadyResolved): + status = "already_resolved" + } + if status != "" { + ackPayload, _ := json.Marshal(resolveAck{ + PendingID: m.PendingID, + Status: status, + OriginID: instanceID, + }) + pubCtx, pubCancel := context.WithTimeout(ctx, 2*time.Second) + if pErr := g.rdb.Publish(pubCtx, m.ReplyChannel, ackPayload).Err(); pErr != nil { + logger.GetLogger(ctx).Warnf("mcp approval pubsub reply: %v", pErr) + } + pubCancel() + } + } + switch { + case err == nil, errors.Is(err, ErrPendingNotFound): + // Either delivered or this isn't the owning replica — quiet. + default: logger.GetLogger(ctx).Warnf("mcp approval pubsub deliver: %v", err) } } _ = sub.Close() - // Reconnect after brief backoff if Redis hiccups. - time.Sleep(2 * time.Second) + // Reconnect with capped exponential backoff if Redis hiccups. + time.Sleep(backoff) + if backoff < maxBackoff { + backoff *= 2 + if backoff > maxBackoff { + backoff = maxBackoff + } + } } } @@ -165,7 +264,14 @@ func (g *Gate) NeedsApproval(ctx context.Context, tenantID uint64, serviceID, to } ok, err := g.checker.IsRequired(ctx, tenantID, serviceID, toolName) if err != nil { - logger.GetLogger(ctx).Warnf("mcp tool approval check failed (skip gate): %v", err) + // Default fail-close: a transient DB error must NOT silently allow a + // dangerous tool to run. Operators can opt into legacy behaviour via + // WEKNORA_AGENT_TOOL_APPROVAL_FAIL_OPEN=true. + if g.failClose { + logger.GetLogger(ctx).Warnf("mcp tool approval check failed (fail-close: requiring approval): %v", err) + return true + } + logger.GetLogger(ctx).Warnf("mcp tool approval check failed (fail-open: skip gate): %v", err) return false } return ok @@ -187,6 +293,7 @@ func (g *Gate) RequestAndWait(ctx context.Context, req PendingRequest) (Decision w := &waiter{ ch: make(chan Decision, 1), tenantID: req.TenantID, + userID: req.UserID, } g.mu.Lock() @@ -273,13 +380,13 @@ func (g *Gate) RequestAndWait(ctx context.Context, req PendingRequest) (Decision return d, nil case <-timer.C: d = Decision{Approved: false, Reason: "approval timeout", TimedOut: true} - w.deliver(d) + _ = w.deliver(d) d = <-w.ch emitResolved(d) return d, nil case <-ctx.Done(): d = Decision{Approved: false, Reason: "request canceled", ContextCanceled: true} - w.deliver(d) + _ = w.deliver(d) d = <-w.ch emitResolved(d) return d, nil @@ -287,48 +394,104 @@ func (g *Gate) RequestAndWait(ctx context.Context, req PendingRequest) (Decision } // Resolve completes a pending approval. tenantID must match the tenant that -// started the wait. If the waiter is not on this instance and Redis Pub/Sub -// is configured, the decision is fanned out to all replicas (best-effort). -func (g *Gate) Resolve(tenantID uint64, pendingID string, d Decision) error { +// started the wait; userID, when non-zero, must match the session owner. +// +// If the waiter is not on this instance and Redis Pub/Sub is configured, the +// decision is fanned out to all replicas and ack'd via a reply key in Redis +// so the caller can distinguish "delivered elsewhere" from "no instance had +// the pending" (issue #1173 follow-up). +func (g *Gate) Resolve(tenantID uint64, userID, pendingID string, d Decision) error { if g == nil { return fmt.Errorf("gate is nil") } - switch err := g.deliverLocal(tenantID, pendingID, d); { + switch err := g.deliverLocal(tenantID, userID, pendingID, d); { case err == nil: return nil - case errors.Is(err, ErrTenantMismatch): + case errors.Is(err, ErrTenantMismatch), + errors.Is(err, ErrUserMismatch), + errors.Is(err, ErrAlreadyResolved): return err case errors.Is(err, ErrPendingNotFound): if g.rdb == nil { return err } - // Fan out to other replicas. - payload, mErr := json.Marshal(resolveMessage{ - TenantID: tenantID, - PendingID: pendingID, - Approved: d.Approved, - ModifiedArgs: d.ModifiedArgs, - Reason: d.Reason, - TimedOut: d.TimedOut, - Canceled: d.ContextCanceled, - }) - if mErr != nil { - return fmt.Errorf("encode pubsub payload: %w", mErr) - } - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - if pErr := g.rdb.Publish(ctx, pubsubChannel, payload).Err(); pErr != nil { - return fmt.Errorf("publish approval resolve: %w", pErr) - } - // Best-effort: the owning instance will deliver and emit Resolved. - return nil + return g.resolveCrossInstance(tenantID, userID, pendingID, d) default: return err } } +// resolveCrossInstance publishes a resolve request and waits briefly for an +// ack from the instance that owns the pending. The ack is sent over a +// per-pending Redis pub/sub reply channel and contains the same error codes +// the local path would return, so HTTP responses stay accurate even when the +// session lives on a different replica. +func (g *Gate) resolveCrossInstance(tenantID uint64, userID, pendingID string, d Decision) error { + replyChannel := pubsubChannel() + ":reply:" + pendingID + sub := g.rdb.Subscribe(context.Background(), replyChannel) + defer func() { _ = sub.Close() }() + // Wait for the subscription to be active before publishing so we don't + // miss the ack. + subCtx, subCancel := context.WithTimeout(context.Background(), 2*time.Second) + defer subCancel() + if _, err := sub.Receive(subCtx); err != nil { + return fmt.Errorf("subscribe approval reply: %w", err) + } + + payload, mErr := json.Marshal(resolveMessage{ + TenantID: tenantID, + UserID: userID, + PendingID: pendingID, + Approved: d.Approved, + ModifiedArgs: d.ModifiedArgs, + Reason: d.Reason, + TimedOut: d.TimedOut, + Canceled: d.ContextCanceled, + ReplyChannel: replyChannel, + OriginID: instanceID, + }) + if mErr != nil { + return fmt.Errorf("encode pubsub payload: %w", mErr) + } + pubCtx, pubCancel := context.WithTimeout(context.Background(), 2*time.Second) + defer pubCancel() + if pErr := g.rdb.Publish(pubCtx, pubsubChannel(), payload).Err(); pErr != nil { + return fmt.Errorf("publish approval resolve: %w", pErr) + } + + // Wait for the owning instance to ack. Window is short (3s) — UX wins + // from getting an accurate 404/409 over slowly returning 200. + ackCtx, ackCancel := context.WithTimeout(context.Background(), 3*time.Second) + defer ackCancel() + for { + msg, err := sub.ReceiveMessage(ackCtx) + if err != nil { + // Timeout or subscription error: nobody confirmed, treat as not found. + return ErrPendingNotFound + } + var ack resolveAck + if jErr := json.Unmarshal([]byte(msg.Payload), &ack); jErr != nil { + continue + } + switch ack.Status { + case "ok": + return nil + case "tenant_mismatch": + return ErrTenantMismatch + case "user_mismatch": + return ErrUserMismatch + case "already_resolved": + return ErrAlreadyResolved + case "not_found": + return ErrPendingNotFound + default: + return fmt.Errorf("approval reply: unexpected status %q", ack.Status) + } + } +} + // deliverLocal attempts to satisfy a waiter on this instance only. -func (g *Gate) deliverLocal(tenantID uint64, pendingID string, d Decision) error { +func (g *Gate) deliverLocal(tenantID uint64, userID, pendingID string, d Decision) error { g.mu.Lock() w, ok := g.pending[pendingID] if !ok { @@ -339,9 +502,19 @@ func (g *Gate) deliverLocal(tenantID uint64, pendingID string, d Decision) error g.mu.Unlock() return ErrTenantMismatch } + if w.userID != "" && userID != "" && w.userID != userID { + g.mu.Unlock() + return ErrUserMismatch + } + already := w.resolved g.mu.Unlock() - w.deliver(d) + if already { + return ErrAlreadyResolved + } + if !w.deliver(d) { + return ErrAlreadyResolved + } return nil } diff --git a/internal/agent/approval/gate_test.go b/internal/agent/approval/gate_test.go index 939c3f05..61ba2330 100644 --- a/internal/agent/approval/gate_test.go +++ b/internal/agent/approval/gate_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "testing" + "time" "github.com/Tencent/WeKnora/internal/config" "github.com/Tencent/WeKnora/internal/event" @@ -43,7 +44,7 @@ func TestGate_RequestAndWait_Approve(t *testing.T) { require.True(t, ok) require.NotEmpty(t, data.PendingID) go func() { - _ = g.Resolve(1, data.PendingID, Decision{Approved: true, ModifiedArgs: json.RawMessage(`{"a":2}`)}) + _ = g.Resolve(1, "", data.PendingID, Decision{Approved: true, ModifiedArgs: json.RawMessage(`{"a":2}`)}) }() return nil }) @@ -78,3 +79,120 @@ func TestGate_NeedsApproval_NoChecker(t *testing.T) { g := NewGate(nil, nil, nil) require.False(t, g.NeedsApproval(context.Background(), 1, "x", "y")) } + +func TestGate_Resolve_NotFound(t *testing.T) { + g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 1}}, &stubChecker{required: true}, nil) + err := g.Resolve(1, "", "no-such-id", Decision{Approved: true}) + require.ErrorIs(t, err, ErrPendingNotFound) +} + +func TestGate_Resolve_TenantMismatch(t *testing.T) { + bus := event.NewEventBus() + g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 2}}, &stubChecker{required: true}, nil) + req := PendingRequest{ + TenantID: 1, EventBus: bus, SessionID: "s1", AssistantMessageID: "m1", + ServiceID: "svc", MCPToolName: "t", Args: json.RawMessage(`{}`), + } + bus.On(event.EventToolApprovalRequired, func(_ context.Context, evt event.Event) error { + data := evt.Data.(event.ToolApprovalRequiredData) + go func() { + require.ErrorIs(t, g.Resolve(999, "", data.PendingID, Decision{Approved: true}), ErrTenantMismatch) + _ = g.Resolve(1, "", data.PendingID, Decision{Approved: false, Reason: "no"}) + }() + return nil + }) + d, err := g.RequestAndWait(context.Background(), req) + require.NoError(t, err) + require.False(t, d.Approved) +} + +func TestGate_Resolve_UserMismatch(t *testing.T) { + bus := event.NewEventBus() + g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 2}}, &stubChecker{required: true}, nil) + req := PendingRequest{ + TenantID: 1, UserID: "alice", EventBus: bus, + SessionID: "s1", AssistantMessageID: "m1", + ServiceID: "svc", MCPToolName: "t", Args: json.RawMessage(`{}`), + } + bus.On(event.EventToolApprovalRequired, func(_ context.Context, evt event.Event) error { + data := evt.Data.(event.ToolApprovalRequiredData) + go func() { + require.ErrorIs(t, g.Resolve(1, "bob", data.PendingID, Decision{Approved: true}), ErrUserMismatch) + _ = g.Resolve(1, "alice", data.PendingID, Decision{Approved: true}) + }() + return nil + }) + d, err := g.RequestAndWait(context.Background(), req) + require.NoError(t, err) + require.True(t, d.Approved) +} + +func TestGate_Resolve_AlreadyResolvedAfterTimeout(t *testing.T) { + g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 1}}, &stubChecker{required: true}, nil) + bus := event.NewEventBus() + + var pendingID string + gotPending := make(chan struct{}, 1) + bus.On(event.EventToolApprovalRequired, func(_ context.Context, evt event.Event) error { + pendingID = evt.Data.(event.ToolApprovalRequiredData).PendingID + gotPending <- struct{}{} + return nil + }) + + go func() { + <-gotPending + // Wait until timeout has fired and the pending entry is still there + // (defer delete only happens after RequestAndWait returns). + // 1s timeout + small slack. + <-time.After(1500 * time.Millisecond) + require.ErrorIs(t, + g.Resolve(1, "", pendingID, Decision{Approved: true}), + ErrPendingNotFound, // entry already removed by RequestAndWait's defer + ) + }() + + d, err := g.RequestAndWait(context.Background(), PendingRequest{ + TenantID: 1, EventBus: bus, SessionID: "s", + ServiceID: "svc", MCPToolName: "t", Args: json.RawMessage(`{}`), + }) + require.NoError(t, err) + require.True(t, d.TimedOut) +} + +func TestGate_Resolve_RaceWinsAlreadyResolved(t *testing.T) { + g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 30}}, &stubChecker{required: true}, nil) + bus := event.NewEventBus() + + type result struct { + first error + second error + } + resCh := make(chan result, 1) + + bus.On(event.EventToolApprovalRequired, func(_ context.Context, evt event.Event) error { + pendingID := evt.Data.(event.ToolApprovalRequiredData).PendingID + go func() { + err1 := g.Resolve(1, "", pendingID, Decision{Approved: true}) + err2 := g.Resolve(1, "", pendingID, Decision{Approved: false}) + resCh <- result{first: err1, second: err2} + }() + return nil + }) + + d, err := g.RequestAndWait(context.Background(), PendingRequest{ + TenantID: 1, EventBus: bus, SessionID: "s", + ServiceID: "svc", MCPToolName: "t", Args: json.RawMessage(`{}`), + }) + require.NoError(t, err) + require.True(t, d.Approved) + r := <-resCh + require.NoError(t, r.first) + // Second call must surface either AlreadyResolved or NotFound (depending + // on whether the defer-delete already ran). + require.True(t, + r.second == nil || // RequestAndWait removed the entry: NotFound is possible too + r.second.Error() == ErrAlreadyResolved.Error() || + r.second.Error() == ErrPendingNotFound.Error(), + "unexpected error: %v", r.second, + ) +} diff --git a/internal/agent/tools/exec_context.go b/internal/agent/tools/exec_context.go index c1666a42..1f1093cc 100644 --- a/internal/agent/tools/exec_context.go +++ b/internal/agent/tools/exec_context.go @@ -2,6 +2,7 @@ package tools import ( "context" + "time" "github.com/Tencent/WeKnora/internal/event" ) @@ -14,11 +15,17 @@ type ToolExecContext struct { AssistantMessageID string RequestID string ToolCallID string + UserID string // owner of the originating session; used by HITL gates for authorization (issue #1173) EventBus *event.EventBus // ApprovalCtx is the parent ctx WITHOUT defaultToolExecTimeout; used when the tool // must wait for human approval that may exceed normal tool exec timeout (issue #1173). // Falls back to the per-tool execCtx when nil. ApprovalCtx context.Context + // ExecTimeout mirrors the per-tool exec timeout the engine applied to the + // outer ctx. Tools that legitimately consume that ctx (e.g. MCP human + // approval) can re-derive a fresh timeout from ApprovalCtx using this + // value instead of hard-coding a duration. Zero means "fallback to 60s". + ExecTimeout time.Duration } // WithToolExecContext returns ctx that carries ToolExecContext for MCP approval and similar features. diff --git a/internal/agent/tools/mcp_tool.go b/internal/agent/tools/mcp_tool.go index e181ce7e..07b7273b 100644 --- a/internal/agent/tools/mcp_tool.go +++ b/internal/agent/tools/mcp_tool.go @@ -118,6 +118,7 @@ func (t *MCPTool) Execute(ctx context.Context, args json.RawMessage) (*types.Too } decision, waitErr := t.gate.RequestAndWait(waitCtx, approval.PendingRequest{ TenantID: tenantID, + UserID: meta.UserID, SessionID: meta.SessionID, AssistantMessageID: meta.AssistantMessageID, RequestID: meta.RequestID, @@ -155,11 +156,15 @@ func (t *MCPTool) Execute(ctx context.Context, args json.RawMessage) (*types.Too }, nil } } - // Approval may have consumed most/all of the per-tool 60s budget set by the + // Approval may have consumed most/all of the per-tool exec budget set by the // agent engine (act.go). Re-derive a fresh tool-exec ctx from ApprovalCtx so // the actual MCP CallTool gets a full timeout window. (issue #1173 follow-up) if meta.ApprovalCtx != nil { - freshCtx, freshCancel := context.WithTimeout(meta.ApprovalCtx, 60*time.Second) + freshTimeout := meta.ExecTimeout + if freshTimeout <= 0 { + freshTimeout = 60 * time.Second + } + freshCtx, freshCancel := context.WithTimeout(meta.ApprovalCtx, freshTimeout) defer freshCancel() ctx = freshCtx } diff --git a/internal/application/repository/mcp_tool_approval_repository.go b/internal/application/repository/mcp_tool_approval_repository.go index 73525383..49fd83d7 100644 --- a/internal/application/repository/mcp_tool_approval_repository.go +++ b/internal/application/repository/mcp_tool_approval_repository.go @@ -10,6 +10,7 @@ import ( "github.com/Tencent/WeKnora/internal/types/interfaces" "github.com/google/uuid" "gorm.io/gorm" + "gorm.io/gorm/clause" ) // MCPToolApprovalRepository implements interfaces.MCPToolApprovalRepository. @@ -51,29 +52,35 @@ func (r *MCPToolApprovalRepository) IsRequired(ctx context.Context, tenantID uin return row.RequireApproval, nil } -// Upsert creates or updates the approval flag for a tool. +// Upsert creates or updates the approval flag for a tool atomically. +// Uses ON CONFLICT against the (tenant_id, service_id, tool_name) unique index +// so concurrent writers don't race the prior SELECT-then-INSERT path into +// duplicate-key 500s. func (r *MCPToolApprovalRepository) Upsert(ctx context.Context, row *types.MCPToolApproval) error { if row == nil { return errors.New("row is nil") } - var existing types.MCPToolApproval - err := r.db.WithContext(ctx). - Where("tenant_id = ? AND service_id = ? AND tool_name = ?", row.TenantID, row.ServiceID, row.ToolName). - First(&existing).Error - if errors.Is(err, gorm.ErrRecordNotFound) { - if row.ID == "" { - row.ID = uuid.New().String() - } - if err := r.db.WithContext(ctx).Create(row).Error; err != nil { - return fmt.Errorf("create mcp tool approval: %w", err) - } - return nil + if row.ID == "" { + row.ID = uuid.New().String() } + now := time.Now() + row.UpdatedAt = now + if row.CreatedAt.IsZero() { + row.CreatedAt = now + } + err := r.db.WithContext(ctx).Clauses(clause.OnConflict{ + Columns: []clause.Column{ + {Name: "tenant_id"}, + {Name: "service_id"}, + {Name: "tool_name"}, + }, + DoUpdates: clause.Assignments(map[string]interface{}{ + "require_approval": row.RequireApproval, + "updated_at": now, + }), + }).Create(row).Error if err != nil { - return fmt.Errorf("get mcp tool approval for upsert: %w", err) + return fmt.Errorf("upsert mcp tool approval: %w", err) } - return r.db.WithContext(ctx).Model(&existing).Updates(map[string]interface{}{ - "require_approval": row.RequireApproval, - "updated_at": time.Now(), - }).Error + return nil } diff --git a/internal/handler/mcp_service.go b/internal/handler/mcp_service.go index 880b20c8..dc2abd86 100644 --- a/internal/handler/mcp_service.go +++ b/internal/handler/mcp_service.go @@ -2,9 +2,10 @@ package handler import ( "encoding/json" + stderrors "errors" "fmt" "net/http" - "net/url" + "strings" "github.com/Tencent/WeKnora/internal/agent/approval" "github.com/Tencent/WeKnora/internal/errors" @@ -472,7 +473,14 @@ func (h *MCPServiceHandler) ListMCPToolApprovals(c *gin.Context) { } rows, err := h.mcpToolApprovalService.ListByService(ctx, tenantID, serviceID) if err != nil { - c.Error(errors.NewNotFoundError(err.Error())) + // Distinguish "service not found" from internal errors so the client + // gets an accurate status code instead of an opaque 404. + if strings.Contains(err.Error(), "not found") { + c.Error(errors.NewNotFoundError(err.Error())) + return + } + logger.ErrorWithFields(ctx, err, map[string]interface{}{"service_id": serviceID}) + c.Error(errors.NewInternalServerError(err.Error())) return } c.JSON(http.StatusOK, gin.H{"success": true, "data": rows}) @@ -486,11 +494,9 @@ type setMCPToolApprovalBody struct { func (h *MCPServiceHandler) SetMCPToolApproval(c *gin.Context) { ctx := c.Request.Context() serviceID := secutils.SanitizeForLog(c.Param("id")) - rawName := c.Param("tool_name") - toolName, err := url.PathUnescape(rawName) - if err != nil { - toolName = rawName - } + // Gin already URL-decodes path params; do not call url.PathUnescape again + // or names containing literal "%" become corrupted. + toolName := c.Param("tool_name") tenantID := c.GetUint64(types.TenantIDContextKey.String()) if tenantID == 0 { c.Error(errors.NewBadRequestError("Tenant ID cannot be empty")) @@ -540,10 +546,14 @@ func (h *MCPServiceHandler) ResolveToolApproval(c *gin.Context) { switch body.Decision { case "approve": dec.Approved = true - if len(body.ModifiedArgs) > 0 { + // Reject "null" / non-object payloads up front. Without this, "null" + // (4 bytes) passes the len>0 check and the downstream tool sees a nil + // argument map, silently losing the original args. + trimmed := strings.TrimSpace(string(body.ModifiedArgs)) + if len(trimmed) > 0 && trimmed != "null" { var probe map[string]interface{} - if err := json.Unmarshal(body.ModifiedArgs, &probe); err != nil { - c.Error(errors.NewBadRequestError("modified_args must be a JSON object")) + if err := json.Unmarshal(body.ModifiedArgs, &probe); err != nil || probe == nil { + c.Error(errors.NewBadRequestError("modified_args must be a non-null JSON object")) return } dec.ModifiedArgs = body.ModifiedArgs @@ -554,17 +564,22 @@ func (h *MCPServiceHandler) ResolveToolApproval(c *gin.Context) { c.Error(errors.NewBadRequestError("decision must be approve or reject")) return } - if err := h.toolApprovalGate.Resolve(tenantID, pendingID, dec); err != nil { - if err == approval.ErrPendingNotFound { + userID, _ := c.Get(types.UserIDContextKey.String()) + userIDStr, _ := userID.(string) + if err := h.toolApprovalGate.Resolve(tenantID, userIDStr, pendingID, dec); err != nil { + switch { + case stderrors.Is(err, approval.ErrPendingNotFound): c.Error(errors.NewNotFoundError("pending approval not found or already completed")) - return - } - if err == approval.ErrTenantMismatch { + case stderrors.Is(err, approval.ErrAlreadyResolved): + c.Error(errors.NewBadRequestError("pending approval already resolved (timeout / cancel raced your action)")) + case stderrors.Is(err, approval.ErrTenantMismatch): c.Error(errors.NewBadRequestError("tenant mismatch")) - return + case stderrors.Is(err, approval.ErrUserMismatch): + c.Error(errors.NewBadRequestError("user mismatch: only the session owner may resolve this approval")) + default: + logger.ErrorWithFields(ctx, err, map[string]interface{}{"pending_id": pendingID}) + c.Error(errors.NewInternalServerError(err.Error())) } - logger.ErrorWithFields(ctx, err, map[string]interface{}{"pending_id": pendingID}) - c.Error(errors.NewInternalServerError(err.Error())) return } c.JSON(http.StatusOK, gin.H{"success": true})