返回 DeepSeek-Reasonix
session.go
根目录 / internal / store / session.go
1 // Package store is the single authority for reasonix's on-disk persistence
2 // layout. Nothing else should construct a persistence path by hand.
3 //
4 // This first slice owns the session-artifact sidecars — the files and
5 // directories that live beside a session's .jsonl (branch metadata, goal state,
6 // checkpoints, background-job artifacts, the cleanup-pending marker). They were
7 // previously derived independently in internal/agent, internal/jobs,
8 // internal/control and internal/acp, each re-spelling the suffix convention; a
9 // layout change meant hunting across packages. Centralizing them here makes
10 // store the one place that knows where a session's artifacts go.
11 //
12 // store is a leaf: it imports only the standard library, so any package may
13 // depend on it without risking an import cycle. Root/directory resolution (the
14 // ~/.reasonix tree) and the desktop root unification land in later slices.
15 package store
16
17 import "strings"
18
19 // studioImportPrefix starts the name of a transcript that Reasonix Studio wrote
20 // into this directory when it imported a session. Studio owns the file and its
21 // sidecars; this runtime neither lists nor maintains them.
22 const studioImportPrefix = "v4-"
23
24 // IsStudioImportName reports whether name belongs to Studio's import of a session.
25 func IsStudioImportName(name string) bool {
26 return strings.HasPrefix(strings.TrimSpace(name), studioImportPrefix)
27 }
28
29 // IsSessionTranscriptName reports whether name is a primary session transcript
30 // file. Append-only event logs and guardian sidecars also end in .jsonl, so
31 // callers that discover sessions by directory scan must use this helper instead
32 // of filepath.Ext.
33 func IsSessionTranscriptName(name string) bool {
34 name = strings.TrimSpace(name)
35 if !strings.HasSuffix(name, ".jsonl") || IsStudioImportName(name) {
36 return false
37 }
38 for _, suffix := range sessionJSONLSidecars {
39 if strings.HasSuffix(name, suffix) {
40 return false
41 }
42 }
43 return true
44 }
45
46 // sessionJSONLSidecars end in .jsonl beside a transcript without being one.
47 // The last three are written by Reasonix 2.x, which shares this directory.
48 var sessionJSONLSidecars = []string{
49 ".events.jsonl", ".turns.jsonl", ".conflicts.jsonl", ".guardian.jsonl",
50 ".wire.jsonl", ".adjudication.jsonl", ".execution.jsonl",
51 }
52
53 // SessionRecoveryState is the persisted Auto-mode recovery checkpoint state
54 // (<id>.recovery.json). It is a regular session-owned sidecar, not a transcript.
55 func SessionRecoveryState(sessionPath string) string {
56 sessionPath = strings.TrimSpace(sessionPath)
57 if sessionPath == "" {
58 return ""
59 }
60 return sessionStem(sessionPath) + ".recovery.json"
61 }
62
63 // SessionTranscriptProjection stores display-only records and their exact
64 // committed event coverage; it never supplies provider-visible messages.
65 func SessionTranscriptProjection(sessionPath string) string {
66 if strings.TrimSpace(sessionPath) == "" {
67 return ""
68 }
69 return sessionStem(sessionPath) + ".transcript-projection.json"
70 }
71
72 // SessionContext is the context-projection / compaction-state sidecar
73 // (<id>.context.json). It holds the model-visible projection and cache
74 // telemetry; transcript authority remains with the native event log once one
75 // exists, with the primary .jsonl retained as its compatibility checkpoint.
76 func SessionContext(sessionPath string) string {
77 sessionPath = strings.TrimSpace(sessionPath)
78 if sessionPath == "" {
79 return ""
80 }
81 return sessionStem(sessionPath) + ".context.json"
82 }
83
84 // SessionPinnedContext is the optional desktop pinned-workspace-context
85 // sidecar (<id>.pinned-context.json). Older versions ignore it while keeping
86 // the primary transcript fully readable.
87 func SessionPinnedContext(sessionPath string) string {
88 sessionPath = strings.TrimSpace(sessionPath)
89 if sessionPath == "" {
90 return ""
91 }
92 return sessionStem(sessionPath) + ".pinned-context.json"
93 }
94
95 // sessionStem strips the .jsonl suffix so a sidecar sits beside the session as
96 // <id>.<kind> rather than <id>.jsonl.<kind>.
97 func sessionStem(sessionPath string) string {
98 return strings.TrimSuffix(sessionPath, ".jsonl")
99 }
100
101 // SessionMeta is the branch-metadata sidecar. Unlike the other sidecars it
102 // appends to the full session path (historical layout), so session.jsonl yields
103 // session.jsonl.meta.
104 func SessionMeta(sessionPath string) string {
105 if sessionPath == "" {
106 return ""
107 }
108 return sessionPath + ".meta"
109 }
110
111 // SessionGoalState is the persisted active-goal sidecar (<id>.goal-state.json).
112 func SessionGoalState(sessionPath string) string {
113 if sessionPath == "" {
114 return ""
115 }
116 return sessionStem(sessionPath) + ".goal-state.json"
117 }
118
119 // SessionEventLog is the append-only transcript event log (<id>.events.jsonl).
120 func SessionEventLog(sessionPath string) string {
121 if sessionPath == "" {
122 return ""
123 }
124 return sessionStem(sessionPath) + ".events.jsonl"
125 }
126
127 // SessionEventLogDamaged is the salvage sidecar for event-log bytes that tail
128 // repair would otherwise discard (<id>.events.jsonl.damaged). It must NOT end
129 // in .jsonl: older binaries scanning a shared session directory classify any
130 // non-excluded .jsonl file as a primary transcript and would resurrect the
131 // damaged bytes as a phantom session.
132 func SessionEventLogDamaged(sessionPath string) string {
133 if sessionPath == "" {
134 return ""
135 }
136 return SessionEventLog(sessionPath) + ".damaged"
137 }
138
139 // SessionEventLogRotating marks a schema-2 log whose rotation is between
140 // reading the old file and publishing the new one; unlocked appenders wait
141 // for it to clear before trusting that their bytes reached the current log.
142 func SessionEventLogRotating(sessionPath string) string {
143 if sessionPath == "" {
144 return ""
145 }
146 return SessionEventLog(sessionPath) + ".rotating"
147 }
148
149 // SessionTurnEventLog is the append-only local runtime lifecycle ledger
150 // (<id>.turns.jsonl). It is independent from the provider transcript so old
151 // readers can continue to consume the primary session unchanged.
152 func SessionTurnEventLog(sessionPath string) string {
153 if sessionPath == "" {
154 return ""
155 }
156 return sessionStem(sessionPath) + ".turns.jsonl"
157 }
158
159 // SessionTurnEventLogDamaged preserves a corrupt/torn ledger tail before the
160 // valid prefix is truncated back into service.
161 func SessionTurnEventLogDamaged(sessionPath string) string {
162 if sessionPath == "" {
163 return ""
164 }
165 return SessionTurnEventLog(sessionPath) + ".damaged"
166 }
167
168 // SessionEventIndex is the listing/checkpoint index for the event log
169 // (<id>.event-index.json). It contains derived offsets and digests, not the
170 // transcript body.
171 func SessionEventIndex(sessionPath string) string {
172 if sessionPath == "" {
173 return ""
174 }
175 return sessionStem(sessionPath) + ".event-index.json"
176 }
177
178 // SessionDisplayIndex is the paging sidecar for the transcript
179 // (<id>.display-index.json). It contains per-message byte offsets, roles, and
180 // turn boundaries derived from the transcript, never message bodies, so a
181 // reader can page a huge history without parsing whole session files.
182 func SessionDisplayIndex(sessionPath string) string {
183 if sessionPath == "" {
184 return ""
185 }
186 return sessionStem(sessionPath) + ".display-index.json"
187 }
188
189 // SessionConflictLog is the append-only diagnostic log for snapshot conflict
190 // recoveries (<id>.conflicts.jsonl). It contains revision counters and branch
191 // ids, not transcript content.
192 func SessionConflictLog(sessionPath string) string {
193 if sessionPath == "" {
194 return ""
195 }
196 return sessionStem(sessionPath) + ".conflicts.jsonl"
197 }
198
199 // SessionLockFile is the advisory save lock (<id>.jsonl.lock).
200 func SessionLockFile(sessionPath string) string {
201 if sessionPath == "" {
202 return ""
203 }
204 return sessionPath + ".lock"
205 }
206
207 // SessionLeaseLock is the runtime ownership lock (<id>.jsonl.lease.lock).
208 func SessionLeaseLock(sessionPath string) string {
209 if sessionPath == "" {
210 return ""
211 }
212 return sessionPath + ".lease.lock"
213 }
214
215 // SessionLeaseInfo is the runtime ownership metadata
216 // (<id>.jsonl.lease.json).
217 func SessionLeaseInfo(sessionPath string) string {
218 if sessionPath == "" {
219 return ""
220 }
221 return sessionPath + ".lease.json"
222 }
223
224 // SessionCheckpointDir is the snapshot-checkpoint directory (<id>.ckpt).
225 func SessionCheckpointDir(sessionPath string) string {
226 if sessionPath == "" {
227 return ""
228 }
229 return sessionStem(sessionPath) + ".ckpt"
230 }
231
232 // SessionJobsDir is the background-job artifact directory (<id>.jobs).
233 func SessionJobsDir(sessionPath string) string {
234 sessionPath = strings.TrimSpace(sessionPath)
235 if sessionPath == "" {
236 return ""
237 }
238 return sessionStem(sessionPath) + ".jobs"
239 }
240
241 // SessionInboxDir is the durable session-level instruction inbox
242 // (<id>.inbox/). Manifest metadata and frozen prompt blobs live here.
243 func SessionInboxDir(sessionPath string) string {
244 sessionPath = strings.TrimSpace(sessionPath)
245 if sessionPath == "" {
246 return ""
247 }
248 return sessionStem(sessionPath) + ".inbox"
249 }
250
251 // SessionCleanupPending is the delayed-cleanup marker (<id>.cleanup-pending.json).
252 func SessionCleanupPending(sessionPath string) string {
253 sessionPath = strings.TrimSpace(sessionPath)
254 if sessionPath == "" {
255 return ""
256 }
257 return sessionStem(sessionPath) + ".cleanup-pending.json"
258 }
259
260 // SessionSidecarFiles returns every regular-file sidecar owned by a session
261 // transcript: branch meta, goal state, event/index logs, pinned context, and
262 // diagnostic logs.
263 // Every surface that deletes a session (desktop trash, /clear, serve, ACP)
264 // must remove all of these — the event log is the authoritative transcript, so
265 // leaving it behind both leaks the "deleted" conversation and lets LoadSession
266 // resurrect it. Directory artifacts (checkpoints, jobs) and ephemeral
267 // lock/lease files have their own lifecycles and are intentionally not listed.
268 func SessionSidecarFiles(sessionPath string) []string {
269 sessionPath = strings.TrimSpace(sessionPath)
270 if sessionPath == "" {
271 return nil
272 }
273 return []string{
274 SessionMeta(sessionPath),
275 SessionGoalState(sessionPath),
276 SessionEventLog(sessionPath),
277 SessionEventLogDamaged(sessionPath),
278 SessionEventLogRotating(sessionPath),
279 SessionTurnEventLog(sessionPath),
280 SessionTurnEventLogDamaged(sessionPath),
281 SessionEventIndex(sessionPath),
282 SessionDisplayIndex(sessionPath),
283 SessionTranscriptProjection(sessionPath),
284 SessionConflictLog(sessionPath),
285 SessionRecoveryState(sessionPath),
286 SessionContext(sessionPath),
287 SessionPinnedContext(sessionPath),
288 }
289 }
290
290 lines GO