返回 DeepSeek-Reasonix
session_source_rows.go
根目录 / desktop / session_source_rows.go
1 package main
2
3 import (
4 "os"
5 "sync"
6
7 "reasonix/desktop/internal/workspacestate"
8 "reasonix/internal/agent"
9 "reasonix/internal/store"
10 )
11
12 type sourceHeadObservation struct {
13 info os.FileInfo
14 index *agent.SessionHeadIndex
15 }
16
17 var sourceHeadRows sync.Map
18
19 // A mapped DAG head must not return as an unidentified path placeholder while
20 // its index is unavailable. This is a display filter, not an ownership alias:
21 // independently identified siblings remain eligible for their own rows.
22 // Adoption belongs to the source, not to the workspace a sidecar lists it in.
23 func adoptedSourceRows(state workspacestate.State) map[string]bool {
24 adopted := map[string]bool{}
25 for _, mapping := range state.SourceMappings {
26 for _, key := range state.SourceKeys(mapping.SourceKey) {
27 adopted["source\x00local\x00"+key] = true
28 }
29 adopted["source\x00local\x00"+desktopSourceKey(mapping.Path, "")] = true
30 if sourceMappingHasPathAlias(mapping) {
31 adopted[sessionRuntimeKey(mapping.Path)] = true
32 }
33 }
34 return adopted
35 }
36
37 // A single-head DAG is displayed by path, while upgrades record its head ID.
38 // Use the same path alias in every projection so retained originals cannot
39 // reappear after their canonical session is archived. Multi-head rows keep
40 // independent identities; adopting one must never hide its siblings.
41 func sourceMappingHasPathAlias(mapping workspacestate.SourceMapping) bool {
42 // A reviewed version is a separate conversation, not another owner of
43 // the original path. Only its versioned SourceKey may alias that session.
44 if historicalVersionSourceKey(mapping.SourceKey) {
45 return false
46 }
47 if mapping.HeadID == "" {
48 return true
49 }
50 heads, err := sessionSourceHeads(mapping.Path)
51 if err != nil {
52 return false
53 }
54 visible := 0
55 selected := false
56 for _, head := range heads {
57 if head.Retired {
58 continue
59 }
60 if head.Kind != agent.HeadKindConcurrent {
61 visible++
62 }
63 if head.Selected && head.ID == mapping.HeadID {
64 selected = true
65 }
66 }
67 return selected && visible <= 1
68 }
69
70 // Listing consumes only the published head index. Replaying an event log here
71 // would make sidebar pagination perform content work and contend with writers.
72 // Missing/stale indices degrade to one path row and are repaired separately.
73 func sessionSourceHeads(path string) ([]agent.SessionHead, error) {
74 if _, err := os.Stat(path); err != nil {
75 sourceHeadRows.Delete(path)
76 return nil, err
77 }
78 info, err := os.Stat(store.SessionEventIndex(path))
79 if err != nil {
80 sourceHeadRows.Delete(path)
81 if os.IsNotExist(err) {
82 return nil, nil
83 }
84 return nil, err
85 }
86 // The checkpoint, event log, and head index are published independently.
87 // Cache only the decoded index, keyed by that file's own identity, and
88 // validate its log coverage on every read. A transient missing/stale index
89 // must not survive publication merely because the checkpoint is unchanged.
90 var index *agent.SessionHeadIndex
91 if cached, ok := sourceHeadRows.Load(path); ok {
92 entry := cached.(sourceHeadObservation)
93 if os.SameFile(entry.info, info) && entry.info.Size() == info.Size() && entry.info.ModTime().Equal(info.ModTime()) {
94 index = entry.index
95 }
96 }
97 if index == nil {
98 index, err = agent.ReadSessionHeadIndex(path)
99 if err != nil || index == nil {
100 sourceHeadRows.Delete(path)
101 return nil, err
102 }
103 sourceHeadRows.Store(path, sourceHeadObservation{info: info, index: index})
104 }
105 if !index.Current(path) {
106 return nil, nil
107 }
108 return index.Heads, nil
109 }
110
111 func expandSessionSourceRows(node ProjectNode) []ProjectNode {
112 if node.Session != nil || node.SessionPath == "" || node.RecoveryState == "recovery_only" {
113 return []ProjectNode{node}
114 }
115 heads, err := sessionSourceHeads(node.SessionPath)
116 if err != nil {
117 node.Health = "degraded"
118 }
119 live := []agent.SessionHead{}
120 for _, head := range heads {
121 if !head.Retired && head.Kind != agent.HeadKindConcurrent {
122 live = append(live, head)
123 }
124 }
125 if len(live) <= 1 {
126 headID := ""
127 if len(live) == 1 {
128 headID = live[0].ID
129 }
130 node.Source = &SessionSourceRef{HostID: localDesktopHostID, Path: node.SessionPath, HeadID: headID, SourceKey: desktopSourceKey(node.SessionPath, headID)}
131 node.Historical = true
132 return []ProjectNode{node}
133 }
134 rows := []ProjectNode{}
135 for _, head := range live {
136 row := node
137 row.Source = &SessionSourceRef{HostID: localDesktopHostID, Path: node.SessionPath, HeadID: head.ID, SourceKey: desktopSourceKey(node.SessionPath, head.ID)}
138 row.Historical, row.HistoricalBranch = true, true
139 row.Key = "source_" + row.Source.SourceKey
140 row.Turns, row.Preview = head.Turns, head.Preview
141 if !head.LastActivity.IsZero() {
142 row.LastActivityAt = head.LastActivity.UnixMilli()
143 }
144 if !head.CreatedAt.IsZero() {
145 row.CreatedAt = head.CreatedAt.UnixMilli()
146 }
147 rows = append(rows, row)
148 }
149 return rows
150 }
151
151 lines GO