返回 DeepSeek-Reasonix
lineage_identity.go
根目录 / internal / sessioncatalog / lineage_identity.go
1 package sessioncatalog
2
3 import (
4 "path/filepath"
5 "strings"
6
7 "reasonix/internal/agent"
8 )
9
10 type groupAnchor struct {
11 topicID string
12 topicTitle string
13 createdAt int64
14 rootIndex int
15 fallbackIndex int
16 }
17
18 func logicalSessionAnchors(records []SessionRecord) map[string]*groupAnchor {
19 anchors := map[string]*groupAnchor{}
20 for i, rec := range records {
21 if !rec.Recovered {
22 id := agent.BranchID(rec.Path)
23 anchors[id] = &groupAnchor{
24 topicID: rec.TopicID, topicTitle: rec.TopicTitle,
25 createdAt: rec.CreatedAt, rootIndex: i, fallbackIndex: i,
26 }
27 }
28 }
29 // Fill missing logical topics from recovered members (root topic empty or
30 // root missing). Prefer earliest non-empty member topic_id.
31 for i, rec := range records {
32 if !rec.Recovered {
33 continue
34 }
35 groupID := strings.TrimSpace(rec.RecoveryGroupID)
36 if groupID == "" {
37 continue
38 }
39 anchor := anchors[groupID]
40 if anchor == nil {
41 anchor = &groupAnchor{rootIndex: -1, createdAt: rec.CreatedAt, fallbackIndex: i}
42 anchors[groupID] = anchor
43 }
44 if anchor.rootIndex < 0 {
45 preferred := pickPreferredRecovery([]SessionRecord{records[anchor.fallbackIndex], rec})
46 if preferred.Path == rec.Path {
47 anchor.fallbackIndex = i
48 }
49 }
50 topic := strings.TrimSpace(rec.TopicID)
51 if topic == "" {
52 continue
53 }
54 if strings.TrimSpace(anchor.topicID) == "" ||
55 (rec.CreatedAt > 0 && (anchor.createdAt == 0 || rec.CreatedAt < anchor.createdAt) &&
56 (anchor.rootIndex < 0 || strings.TrimSpace(records[anchor.rootIndex].TopicID) == "")) {
57 // Keep a non-empty root topic when present; only override empty roots.
58 if anchor.rootIndex >= 0 && strings.TrimSpace(records[anchor.rootIndex].TopicID) != "" {
59 continue
60 }
61 anchor.topicID = topic
62 anchor.topicTitle = rec.TopicTitle
63 if rec.CreatedAt > 0 {
64 anchor.createdAt = rec.CreatedAt
65 }
66 }
67 }
68 for _, anchor := range anchors {
69 if strings.TrimSpace(anchor.topicID) == "" {
70 // Content proof must retain metadata discovery's legacy identity.
71 // With no surviving root, use the ordinary recovery representative;
72 // every sibling still folds onto that one topic without sidecar writes.
73 path := records[anchor.fallbackIndex].Path
74 anchor.topicID = agent.LegacySessionTopicID(path)
75 if anchor.topicTitle == "" {
76 anchor.topicTitle = strings.TrimSuffix(filepath.Base(path), ".jsonl")
77 }
78 }
79 }
80 return anchors
81 }
82
82 lines GO