Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): stop polling expired webhook traces (#20619)

Guofang.Tangb88e35b7ecf8
diff --git a/internal/handler/agent_webhook_trace.go b/internal/handler/agent_webhook_trace.go
index 480f007099..28bcf95a00 100644
--- a/internal/handler/agent_webhook_trace.go
+++ b/internal/handler/agent_webhook_trace.go
@@ -56,15 +56,14 @@ func parseWebhookSinceTS(raw string) (float64, bool) {
}
// pollWebhookTrace advances one webhook trace polling step.
+// Missing data ends polling for a selected run without implying execution success.
func pollWebhookTrace(raw string, sinceTS float64, webhookID string) (webhookTracePoll, error) {
empty := newWebhookTracePoll(nil, sinceTS, false)
- if strings.TrimSpace(raw) == "" {
- return empty, nil
- }
-
var store webhookTraceStore
- if err := json.Unmarshal([]byte(raw), &store); err != nil {
- return webhookTracePoll{}, fmt.Errorf("decode webhook trace: %w", err)
+ if strings.TrimSpace(raw) != "" {
+ if err := json.Unmarshal([]byte(raw), &store); err != nil {
+ return webhookTracePoll{}, fmt.Errorf("decode webhook trace: %w", err)
+ }
}
if webhookID == "" {
startKey, startTS, ok := nextWebhookTraceRun(store.Webhooks, sinceTS)
diff --git a/internal/handler/agent_webhook_trace_poll_test.go b/internal/handler/agent_webhook_trace_poll_test.go
new file mode 100644
index 0000000000..de2668f854
--- /dev/null
+++ b/internal/handler/agent_webhook_trace_poll_test.go
@@ -0,0 +1,58 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package handler
+
+import "testing"
+
+// TestPollWebhookTraceMissingRun distinguishes waiting for a trigger from losing an already selected trace.
+func TestPollWebhookTraceMissingRun(t *testing.T) {
+ id := encodeWebhookID("50")
+ tests := []struct {
+ name string
+ raw string
+ webhookID string
+ finished bool
+ }{
+ {"waiting without logs", "", "", false},
+ {"waiting with blank logs", " \n", "", false},
+ {"expired selected trace", "", id, true},
+ {"blank selected trace", " \n", id, true},
+ {"empty store", `{"webhooks":{}}`, id, true},
+ {"another run remains", `{"webhooks":{"60":{"start_ts":60,"events":[]}}}`, id, true},
+ {"selected run still active", `{"webhooks":{"50":{"start_ts":50,"events":[]}}}`, id, false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result, err := pollWebhookTrace(tt.raw, 51, tt.webhookID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if result.Finished != tt.finished || result.NextSinceTS != 51 {
+ t.Fatalf("result = %+v, want finished=%t with unchanged cursor", result, tt.finished)
+ }
+ if tt.webhookID == "" {
+ if result.WebhookID != nil {
+ t.Fatalf("unexpected webhook ID: %v", result.WebhookID)
+ }
+ } else if result.WebhookID == nil || *result.WebhookID != tt.webhookID {
+ t.Fatalf("result = %+v, want selected webhook ID preserved", result)
+ }
+ // Ending trace polling must not synthesize a successful execution event.
+ if result.Events == nil || len(result.Events) != 0 {
+ t.Fatalf("events = %#v, want an empty non-nil slice", result.Events)
+ }
+ })
+ }
+}
diff --git a/internal/handler/agent_webhook_trace_test.go b/internal/handler/agent_webhook_trace_test.go
index 4eb2fb989f..6033415672 100644
--- a/internal/handler/agent_webhook_trace_test.go
+++ b/internal/handler/agent_webhook_trace_test.go
@@ -399,6 +399,41 @@ func TestGetAgentWebhookLogsHandlesMissingAndInvalidState(t *testing.T) {
}
}
+// TestGetAgentWebhookLogsStopsAfterExpiry checks the lifecycle of an expired selected trace.
+func TestGetAgentWebhookLogsStopsAfterExpiry(t *testing.T) {
+ h, rdb := newWebhookTraceTestHandler(t)
+ seedWebhookTrace(t, rdb, "c1", map[string]any{
+ "50": map[string]any{"start_ts": 50, "events": []any{}},
+ })
+ discovery := requestWebhookTrace(t, h, "c1", "u1", url.Values{"since_ts": {"42"}})
+ if discovery.Data == nil || discovery.Data.WebhookID == nil || discovery.Data.Finished {
+ t.Fatalf("discovery = %+v, want an active trace", discovery)
+ }
+ id := *discovery.Data.WebhookID
+ query := url.Values{"since_ts": {"50"}, "webhook_id": {id}}
+ key := "webhook-trace-c1-logs"
+ // Expire deterministically without sleeping or changing the wall clock.
+ if err := rdb.ExpireAt(t.Context(), key, time.Unix(1, 0)).Err(); err != nil {
+ t.Fatal(err)
+ }
+ if exists, err := rdb.Exists(t.Context(), key).Result(); err != nil || exists != 0 {
+ t.Fatalf("trace key exists = %d, err = %v, want expired", exists, err)
+ }
+ for range 2 {
+ poll := requestWebhookTrace(t, h, "c1", "u1", query)
+ if poll.Code != int(common.CodeSuccess) || poll.Data == nil {
+ t.Fatalf("expired trace response = %+v", poll)
+ }
+ data := poll.Data
+ if !data.Finished || data.WebhookID == nil || *data.WebhookID != id || data.NextSinceTS != 50 {
+ t.Fatalf("expired trace = %+v, want finished with original ID and cursor", data)
+ }
+ if data.Events == nil || len(data.Events) != 0 {
+ t.Fatalf("expired trace events = %#v, want []", data.Events)
+ }
+ }
+}
+
// TestGetAgentWebhookLogsRedactsRedisFailures covers corrupt data and backend errors.
func TestGetAgentWebhookLogsRedactsRedisFailures(t *testing.T) {
h, rdb := newWebhookTraceTestHandler(t)