|
1 | 1 | import { |
2 | 2 | loadSessionEntry, |
3 | 3 | replaceSessionEntrySync, |
| 4 | + replaceTranscriptEventsSync, |
| 5 | + withTranscriptWriteTransaction, |
4 | 6 | } from "../../config/sessions/session-accessor.js"; |
5 | 7 | import { projectCanonicalSessionEntryShape } from "../../config/sessions/store-entry-shape.js"; |
6 | 8 | import { CURRENT_SESSION_VERSION } from "../../config/sessions/version.js"; |
@@ -102,7 +104,7 @@ export class SessionManagerBranching extends SessionManagerEntries { |
102 | 104 | return { entries, opaqueEntries, tailId, usedIds }; |
103 | 105 | } |
104 | 106 |
|
105 | | - createBranchedSession(leafId: string): string | undefined { |
| 107 | + async createBranchedSession(leafId: string): Promise<string | undefined> { |
106 | 108 | const previousSessionId = this.sessionId; |
107 | 109 | const branchPath = this.collectBranchedSessionPath(leafId); |
108 | 110 | if (branchPath.entries.length === 0) { |
@@ -152,35 +154,63 @@ export class SessionManagerBranching extends SessionManagerEntries { |
152 | 154 | this.fileEntries = [header, ...branchPath.entries, ...labelEntries]; |
153 | 155 | this.opaqueFileEntries = branchPath.opaqueEntries; |
154 | 156 | this.sessionId = newSessionId; |
155 | | - if (persistenceTarget) { |
156 | | - const updatedAt = Date.now(); |
157 | | - const previousEntry = loadSessionEntry({ |
158 | | - agentId: persistenceTarget.agentId, |
159 | | - sessionKey: persistenceTarget.sessionKey, |
160 | | - storePath: persistenceTarget.storePath, |
161 | | - }); |
162 | | - const canonicalPreviousEntry = previousEntry |
163 | | - ? projectCanonicalSessionEntryShape(previousEntry as unknown as Record<string, unknown>) |
164 | | - : { updatedAt }; |
165 | | - this.persistenceTarget = { ...persistenceTarget, sessionId: newSessionId }; |
166 | | - replaceSessionEntrySync( |
167 | | - { |
168 | | - agentId: persistenceTarget.agentId, |
169 | | - sessionKey: persistenceTarget.sessionKey, |
170 | | - storePath: persistenceTarget.storePath, |
171 | | - }, |
172 | | - { |
173 | | - ...canonicalPreviousEntry, |
174 | | - sessionId: newSessionId, |
175 | | - updatedAt, |
176 | | - }, |
177 | | - ); |
178 | | - this.buildIndex(); |
179 | | - this.replacePersistedTranscript(); |
180 | | - return newSessionId; |
| 157 | + this.buildIndex(); |
| 158 | + if (!persistenceTarget) { |
| 159 | + return undefined; |
181 | 160 | } |
182 | 161 |
|
183 | | - this.buildIndex(); |
184 | | - return undefined; |
| 162 | + const entryScope = { |
| 163 | + agentId: persistenceTarget.agentId, |
| 164 | + sessionKey: persistenceTarget.sessionKey, |
| 165 | + storePath: persistenceTarget.storePath, |
| 166 | + }; |
| 167 | + const previousEntry = loadSessionEntry(entryScope); |
| 168 | + const updatedAt = Date.now(); |
| 169 | + const nextTarget = { ...persistenceTarget, sessionId: newSessionId }; |
| 170 | + const nextEntry = { |
| 171 | + ...(previousEntry |
| 172 | + ? projectCanonicalSessionEntryShape(previousEntry as unknown as Record<string, unknown>) |
| 173 | + : { updatedAt }), |
| 174 | + sessionId: newSessionId, |
| 175 | + updatedAt, |
| 176 | + }; |
| 177 | + try { |
| 178 | + const persisted = await withTranscriptWriteTransaction(persistenceTarget, () => { |
| 179 | + const currentEntry = loadSessionEntry(entryScope); |
| 180 | + if ( |
| 181 | + currentEntry?.sessionId !== previousSessionId || |
| 182 | + currentEntry.lifecycleRevision !== previousEntry?.lifecycleRevision |
| 183 | + ) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + replaceSessionEntrySync(entryScope, nextEntry); |
| 187 | + if (!replaceTranscriptEventsSync(nextTarget, this.getPersistedFileEntries())) { |
| 188 | + throw new Error("Branched session transcript was not persisted"); |
| 189 | + } |
| 190 | + return true; |
| 191 | + }); |
| 192 | + if (!persisted) { |
| 193 | + const actualEntry = loadSessionEntry(entryScope); |
| 194 | + const cause = actualEntry |
| 195 | + ? { |
| 196 | + actualSessionId: actualEntry.sessionId, |
| 197 | + code: "session-rebound" as const, |
| 198 | + expectedSessionId: previousSessionId, |
| 199 | + sessionKey: persistenceTarget.sessionKey, |
| 200 | + } |
| 201 | + : { |
| 202 | + code: "session-entry-missing" as const, |
| 203 | + expectedSessionId: previousSessionId, |
| 204 | + sessionKey: persistenceTarget.sessionKey, |
| 205 | + }; |
| 206 | + throw new Error(`Branched session was not persisted: ${cause.code}`, { cause }); |
| 207 | + } |
| 208 | + } catch (error) { |
| 209 | + this.setSessionTarget(persistenceTarget); |
| 210 | + throw error; |
| 211 | + } |
| 212 | + this.persistenceTarget = nextTarget; |
| 213 | + this.persistenceHeaderPending = false; |
| 214 | + return newSessionId; |
185 | 215 | } |
186 | 216 | } |
0 commit comments