Skip to content

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

fix(memory): restore Python-side rules in Go TYPE_INSTRUCTIONS (#19002)

Charles8f1fd9b1f503
diff --git a/internal/service/memory.go b/internal/service/memory.go
index 7006bdb367..a9ec3f9a07 100644
--- a/internal/service/memory.go
+++ b/internal/service/memory.go
@@ -121,34 +121,45 @@ You are an expert at analyzing conversations to extract structured memory.
6. Maximum {max_items} items per type
`
-// TYPE_INSTRUCTIONS contains specific instructions for each memory type extraction
+// TYPE_INSTRUCTIONS contains specific instructions for each memory type extraction.
+// Kept in lockstep with Python's memory/utils/prompt_util.py TYPE_INSTRUCTIONS —
+// both implementations drive the same extraction contract, and a rule present on
+// only one side (e.g. the semantic Default line) drifts extraction behavior across
+// languages (see #18415).
var TYPE_INSTRUCTIONS = map[string]string{
"semantic": `
**EXTRACT SEMANTIC KNOWLEDGE:**
- Universal facts, definitions, concepts, relationships
- Time-invariant, generally true information
+- Examples: "The capital of France is Paris", "Water boils at 100°C"
-**Timestamp Rules:**
-- valid_at: When the fact became true
-- invalid_at: When it becomes false or empty if still true
+**Timestamp Rules for Semantic Knowledge:**
+- valid_at: When the fact became true (e.g., law enactment, discovery)
+- invalid_at: When it becomes false (e.g., repeal, disproven) or empty if still true
+- Default: valid_at = conversation time, invalid_at = "" for timeless facts
`,
"episodic": `
**EXTRACT EPISODIC KNOWLEDGE:**
- Specific experiences, events, personal stories
- Time-bound, person-specific, contextual
+- Examples: "Yesterday I fixed the bug", "User reported issue last week"
-**Timestamp Rules:**
+**Timestamp Rules for Episodic Knowledge:**
- valid_at: Event start/occurrence time
- invalid_at: Event end time or empty if instantaneous
+- Extract explicit times: "at 3 PM", "last Monday", "from X to Y"
`,
"procedural": `
**EXTRACT PROCEDURAL KNOWLEDGE:**
- Processes, methods, step-by-step instructions
- Goal-oriented, actionable, often includes conditions
+- Examples: "To reset password, click...", "Debugging steps: 1)..."
-**Timestamp Rules:**
+**Timestamp Rules for Procedural Knowledge:**
- valid_at: When procedure becomes valid/effective
- invalid_at: When it expires/becomes obsolete or empty if current
+- For version-specific: use release dates
+- For best practices: invalid_at = ""
`,
}
diff --git a/internal/service/memory_extractor_test.go b/internal/service/memory_extractor_test.go
index 697ce3f8f0..8ea40384df 100644
--- a/internal/service/memory_extractor_test.go
+++ b/internal/service/memory_extractor_test.go
@@ -20,6 +20,7 @@
package service
import (
+ "strings"
"testing"
"time"
)
@@ -93,3 +94,43 @@ func TestBuildExtractedMessageValidAtSemantics(t *testing.T) {
t.Fatalf("valid_at = %v, want fallback %q", got, now.Format(memoryTimeLayout))
}
}
+
+// TestTypeInstructionsStayInLockstepWithPython pins the rules that
+// memory/utils/prompt_util.py carries and the Go map historically dropped:
+// per-type Examples lines, the full "Timestamp Rules for <Type> Knowledge:"
+// titles, and the semantic Default line. If a substring fails here, the two
+// implementations are handing the extracting LLM different contracts (the
+// drift this map was merged with — see #18415).
+func TestTypeInstructionsStayInLockstepWithPython(t *testing.T) {
+ want := map[string][]string{
+ "semantic": {
+ "- Examples: \"The capital of France is Paris\", \"Water boils at 100°C\"",
+ "**Timestamp Rules for Semantic Knowledge:**",
+ "- valid_at: When the fact became true (e.g., law enactment, discovery)",
+ "- invalid_at: When it becomes false (e.g., repeal, disproven) or empty if still true",
+ "- Default: valid_at = conversation time, invalid_at = \"\" for timeless facts",
+ },
+ "episodic": {
+ "- Examples: \"Yesterday I fixed the bug\", \"User reported issue last week\"",
+ "**Timestamp Rules for Episodic Knowledge:**",
+ "- Extract explicit times: \"at 3 PM\", \"last Monday\", \"from X to Y\"",
+ },
+ "procedural": {
+ "- Examples: \"To reset password, click...\", \"Debugging steps: 1)...\"",
+ "**Timestamp Rules for Procedural Knowledge:**",
+ "- For version-specific: use release dates",
+ "- For best practices: invalid_at = \"\"",
+ },
+ }
+ for memoryType, lines := range want {
+ got, ok := TYPE_INSTRUCTIONS[memoryType]
+ if !ok {
+ t.Fatalf("TYPE_INSTRUCTIONS has no entry for %q", memoryType)
+ }
+ for _, line := range lines {
+ if !strings.Contains(got, line) {
+ t.Errorf("TYPE_INSTRUCTIONS[%q] is missing the Python-side rule: %s", memoryType, line)
+ }
+ }
+ }
+}