Skip to content

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

fix(harness): preserve message fields and isolate agentic content (#20869)

adws7d7b784e86bd
diff --git a/internal/harness/core/state_guard.go b/internal/harness/core/state_guard.go
index 7db3624a0e..31fea22c5b 100644
--- a/internal/harness/core/state_guard.go
+++ b/internal/harness/core/state_guard.go
@@ -38,20 +38,16 @@ func copyMessage[M MessageType](msg M) M {
switch v := any(msg).(type) {
case *schema.Message:
cp := &schema.Message{
- Role: v.Role,
- Content: v.Content,
- Name: v.Name,
+ Role: v.Role,
+ Content: v.Content,
+ Name: v.Name,
+ ToolName: v.ToolName,
}
if len(v.ToolCalls) > 0 {
cp.ToolCalls = make([]schema.ToolCall, len(v.ToolCalls))
copy(cp.ToolCalls, v.ToolCalls)
}
- if v.Extra != nil {
- cp.Extra = make(map[string]any, len(v.Extra))
- for k, val := range v.Extra {
- cp.Extra[k] = deepCopyAny(val)
- }
- }
+ cp.Extra = copyExtra(v.Extra)
return any(cp).(M)
case *schema.AgenticMessage:
cp := &schema.AgenticMessage{
@@ -61,12 +57,41 @@ func copyMessage[M MessageType](msg M) M {
if len(v.ContentBlocks) > 0 {
cp.ContentBlocks = make([]schema.ContentBlock, len(v.ContentBlocks))
copy(cp.ContentBlocks, v.ContentBlocks)
+ for i, block := range v.ContentBlocks {
+ if block.ToolCall != nil {
+ cp.ContentBlocks[i].ToolCall = &schema.ToolCallInfo{
+ ID: block.ToolCall.ID,
+ Name: block.ToolCall.Name,
+ Arguments: block.ToolCall.Arguments,
+ }
+ }
+ if block.ToolResult != nil {
+ cp.ContentBlocks[i].ToolResult = &schema.ToolResult{
+ ToolCallID: block.ToolResult.ToolCallID,
+ Name: block.ToolResult.Name,
+ Content: block.ToolResult.Content,
+ Error: block.ToolResult.Error,
+ Extra: copyExtra(block.ToolResult.Extra),
+ }
+ }
+ }
}
return any(cp).(M)
}
return msg
}
+func copyExtra(extra map[string]any) map[string]any {
+ if extra == nil {
+ return nil
+ }
+ copied := make(map[string]any, len(extra))
+ for k, val := range extra {
+ copied[k] = deepCopyAny(val)
+ }
+ return copied
+}
+
// deepCopyAny performs a deep copy of an arbitrary value via JSON round-trip.
// Falls back to the original value if JSON marshal/unmarshal fails.
func deepCopyAny(v any) any {
diff --git a/internal/harness/core/state_guard_test.go b/internal/harness/core/state_guard_test.go
new file mode 100644
index 0000000000..73e2dc9751
--- /dev/null
+++ b/internal/harness/core/state_guard_test.go
@@ -0,0 +1,67 @@
+package core
+
+import (
+ "reflect"
+ "testing"
+
+ "ragflow/internal/harness/core/schema"
+)
+
+func TestCopyMessagePreservesToolName(t *testing.T) {
+ original := &schema.Message{
+ Role: schema.RoleTool,
+ Content: "result",
+ Name: "call-1",
+ ToolName: "lookup",
+ }
+ if copied := copyMessage(original); !reflect.DeepEqual(copied, original) {
+ t.Fatalf("copied message = %+v, want %+v", copied, original)
+ }
+}
+
+func TestCopyMessageAgenticIsolatesContentBlocks(t *testing.T) {
+ call := &schema.ToolCallInfo{Name: "lookup", Arguments: `{}`}
+ nested := map[string]any{"value": "original", "items": []any{"original"}}
+ result := &schema.ToolResult{
+ Content: "result",
+ Extra: map[string]any{"nested": nested},
+ }
+ original := &schema.AgenticMessage{
+ Role: schema.AgenticRoleAssistant,
+ Content: "response",
+ ContentBlocks: []schema.ContentBlock{
+ {Type: "text", Text: "original"},
+ {Type: "tool_use", ToolCall: call},
+ {Type: "tool_result", ToolResult: result},
+ },
+ }
+ copied := copyMessage(original)
+ if !reflect.DeepEqual(copied, original) {
+ t.Fatal("copy changed agentic message fields")
+ }
+
+ copied.ContentBlocks[0].Text = "changed"
+ copied.ContentBlocks[1].ToolCall.Arguments = "changed"
+ copied.ContentBlocks[2].ToolResult.Content = "changed"
+ extra := copied.ContentBlocks[2].ToolResult.Extra
+ extra["new"] = "changed"
+ copiedNested := extra["nested"].(map[string]any)
+ copiedNested["value"] = "changed"
+ copiedNested["items"].([]any)[0] = "changed"
+
+ if original.ContentBlocks[0].Text != "original" {
+ t.Error("copy shares content blocks")
+ }
+ if call.Arguments != `{}` {
+ t.Error("copy shares tool call")
+ }
+ if result.Content != "result" {
+ t.Error("copy shares tool result")
+ }
+ if len(result.Extra) != 1 {
+ t.Error("copy shares tool result extra map")
+ }
+ if nested["value"] != "original" || nested["items"].([]any)[0] != "original" {
+ t.Error("copy shares nested tool result extra values")
+ }
+}