@@ -91,187 +91,118 @@ type SqliteRestoreCheckpointSessionParams = {
9191 legacySource ?: SqliteCompactionCheckpointLegacySource ;
9292} ;
9393
94+ type SqliteCompactionCheckpointSessionOperation =
95+ | ( { kind : "branch" } & SqliteBranchCheckpointSessionParams )
96+ | ( { kind : "restore" } & SqliteRestoreCheckpointSessionParams ) ;
97+
9498export async function branchCompactionCheckpointSession (
9599 params : SqliteBranchCheckpointSessionParams ,
96100) : Promise < SqliteCompactionCheckpointSessionMutationResult > {
97- const sourceKey = normalizeSqliteSessionKey ( params . sourceStoreKey ?? params . sourceKey ) ;
98- const requestedSourceKey = normalizeSqliteSessionKey ( params . sourceKey ) ;
99- const targetKey = normalizeSqliteSessionKey ( params . nextKey ) ;
100- const resolved = resolveSqliteScope ( {
101- ...( params . agentId ? { agentId : params . agentId } : { } ) ,
102- ...( params . env ? { env : params . env } : { } ) ,
103- sessionKey : sourceKey ,
104- ...( params . storePath ? { storePath : params . storePath } : { } ) ,
105- } ) ;
106- return await runExclusiveSqliteSessionWrite ( resolved , async ( ) => {
107- let result : SqliteCompactionCheckpointSessionMutationResult | undefined ;
108- let previousIdentity = new Map < string , SessionEntry > ( ) ;
109- let currentIdentity = new Map < string , SessionEntry > ( ) ;
110- runOpenClawAgentWriteTransaction ( ( database ) => {
111- const identityKeys = uniqueStrings ( [
112- ...collectSessionEntryLookupKeys ( database , sourceKey ) ,
113- ...collectSessionEntryLookupKeys ( database , targetKey ) ,
114- ] ) ;
115- previousIdentity = readSessionIdentitySnapshot ( database , identityKeys ) ;
116- result = branchSqliteCompactionCheckpointSessionInTransaction ( database , {
117- checkpointId : params . checkpointId ,
118- expectedState : params . expectedState ,
119- parentSessionKey : requestedSourceKey ,
120- legacySource : params . legacySource ,
121- resolved,
122- sourceKey,
123- targetKey,
124- } ) ;
125- currentIdentity = readSessionIdentitySnapshot ( database , identityKeys ) ;
126- } , toDatabaseOptions ( resolved ) ) ;
127- emitCommittedSessionIdentityDiff ( previousIdentity , currentIdentity ) ;
128- return result ?? { status : "failed" } ;
129- } ) ;
101+ return await applySqliteCompactionCheckpointSessionOperation ( { ...params , kind : "branch" } ) ;
130102}
131103
132- /** Restores a SQLite session from a compaction checkpoint in one queued transaction. */
133104export async function restoreCompactionCheckpointSession (
134105 params : SqliteRestoreCheckpointSessionParams ,
135106) : Promise < SqliteCompactionCheckpointSessionMutationResult > {
136- const sessionKey = normalizeSqliteSessionKey ( params . sessionStoreKey ?? params . sessionKey ) ;
137- const targetKey = normalizeSqliteSessionKey ( params . sessionKey ) ;
107+ return await applySqliteCompactionCheckpointSessionOperation ( { ...params , kind : "restore" } ) ;
108+ }
109+
110+ async function applySqliteCompactionCheckpointSessionOperation (
111+ operation : SqliteCompactionCheckpointSessionOperation ,
112+ ) : Promise < SqliteCompactionCheckpointSessionMutationResult > {
113+ const sourceKey = normalizeSqliteSessionKey (
114+ operation . kind === "branch"
115+ ? ( operation . sourceStoreKey ?? operation . sourceKey )
116+ : ( operation . sessionStoreKey ?? operation . sessionKey ) ,
117+ ) ;
118+ const targetKey = normalizeSqliteSessionKey (
119+ operation . kind === "branch" ? operation . nextKey : operation . sessionKey ,
120+ ) ;
138121 const resolved = resolveSqliteScope ( {
139- ...( params . agentId ? { agentId : params . agentId } : { } ) ,
140- ...( params . env ? { env : params . env } : { } ) ,
141- sessionKey,
142- ...( params . storePath ? { storePath : params . storePath } : { } ) ,
122+ ...( operation . agentId ? { agentId : operation . agentId } : { } ) ,
123+ ...( operation . env ? { env : operation . env } : { } ) ,
124+ sessionKey : sourceKey ,
125+ ...( operation . storePath ? { storePath : operation . storePath } : { } ) ,
143126 } ) ;
144127 return await runExclusiveSqliteSessionWrite ( resolved , async ( ) => {
145- let result : SqliteCompactionCheckpointSessionMutationResult | undefined ;
146- let previousIdentity = new Map < string , SessionEntry > ( ) ;
147- let currentIdentity = new Map < string , SessionEntry > ( ) ;
148- runOpenClawAgentWriteTransaction ( ( database ) => {
128+ const committed = runOpenClawAgentWriteTransaction ( ( database ) => {
149129 const identityKeys = uniqueStrings ( [
150- ...collectSessionEntryLookupKeys ( database , sessionKey ) ,
130+ ...collectSessionEntryLookupKeys ( database , sourceKey ) ,
151131 ...collectSessionEntryLookupKeys ( database , targetKey ) ,
152132 ] ) ;
153- previousIdentity = readSessionIdentitySnapshot ( database , identityKeys ) ;
154- result = restoreSqliteCompactionCheckpointSessionInTransaction ( database , {
155- checkpointId : params . checkpointId ,
156- expectedState : params . expectedState ,
157- legacySource : params . legacySource ,
133+ const previousIdentity = readSessionIdentitySnapshot ( database , identityKeys ) ;
134+ const result = applySqliteCompactionCheckpointSessionOperationInTransaction (
135+ database ,
158136 resolved ,
159- sourceKey : sessionKey ,
137+ operation ,
138+ sourceKey ,
160139 targetKey ,
161- } ) ;
162- currentIdentity = readSessionIdentitySnapshot ( database , identityKeys ) ;
140+ ) ;
141+ return {
142+ previousIdentity,
143+ currentIdentity : readSessionIdentitySnapshot ( database , identityKeys ) ,
144+ result,
145+ } ;
163146 } , toDatabaseOptions ( resolved ) ) ;
164- emitCommittedSessionIdentityDiff ( previousIdentity , currentIdentity ) ;
165- return result ?? { status : "failed" } ;
147+ emitCommittedSessionIdentityDiff ( committed . previousIdentity , committed . currentIdentity ) ;
148+ return committed . result ;
166149 } ) ;
167150}
168151
169- /** Publishes a transcript update using the SQLite transcript scope target. */
170-
171- function branchSqliteCompactionCheckpointSessionInTransaction (
152+ function applySqliteCompactionCheckpointSessionOperationInTransaction (
172153 database : OpenClawAgentDatabase ,
173- params : {
174- checkpointId : string ;
175- expectedState : SessionEntryExpectedState ;
176- legacySource ?: SqliteCompactionCheckpointLegacySource ;
177- parentSessionKey : string ;
178- resolved : ResolvedSqliteScope ;
179- sourceKey : string ;
180- targetKey : string ;
181- } ,
154+ resolved : ResolvedSqliteScope ,
155+ operation : SqliteCompactionCheckpointSessionOperation ,
156+ sourceKey : string ,
157+ targetKey : string ,
182158) : SqliteCompactionCheckpointSessionMutationResult {
183- const currentEntry = readSessionEntryRow ( database , params . sourceKey ) ?. entry ;
159+ const currentEntry = readSessionEntryRow ( database , sourceKey ) ?. entry ;
184160 if ( ! currentEntry ?. sessionId ) {
185161 return { status : "missing-session" } ;
186162 }
187163 if (
188- currentEntry . sessionId !== params . expectedState . sessionId ||
189- currentEntry . lifecycleRevision !== params . expectedState . lifecycleRevision
164+ currentEntry . sessionId !== operation . expectedState . sessionId ||
165+ currentEntry . lifecycleRevision !== operation . expectedState . lifecycleRevision
190166 ) {
191167 return { status : "conflict" } ;
192168 }
193169 if ( currentEntry . modelSelectionLocked === true ) {
194170 return { status : "model-selection-locked" } ;
195171 }
196- const checkpoint = readSessionCompactionCheckpoint ( currentEntry , params . checkpointId ) ;
172+ const checkpoint = readSessionCompactionCheckpoint ( currentEntry , operation . checkpointId ) ;
197173 if ( ! checkpoint ) {
198174 return { status : "missing-checkpoint" } ;
199175 }
200- const forked = forkSqliteCheckpointTranscriptInTransaction ( database , params . resolved , {
176+ const forked = forkSqliteCheckpointTranscriptInTransaction ( database , resolved , {
201177 checkpoint,
202- legacySource : params . legacySource ,
203- targetSessionKey : params . targetKey ,
178+ legacySource : operation . legacySource ,
179+ targetSessionKey : targetKey ,
204180 } ) ;
205181 if ( forked . status !== "created" ) {
206182 return forked ;
207183 }
208184
209- const label = currentEntry . label ?. trim ( )
210- ? `${ currentEntry . label . trim ( ) } (checkpoint)`
211- : "Checkpoint branch" ;
212- const nextEntry = cloneSqliteCheckpointSessionEntry ( {
213- currentEntry,
214- label,
215- nextSessionId : forked . sessionId ,
216- parentSessionKey : params . parentSessionKey ,
217- totalTokens : forked . totalTokens ,
218- } ) ;
219- writeSessionEntry ( database , params . targetKey , nextEntry ) ;
185+ const nextEntry =
186+ operation . kind === "branch"
187+ ? cloneSqliteCheckpointSessionEntry ( {
188+ currentEntry,
189+ label : currentEntry . label ?. trim ( )
190+ ? `${ currentEntry . label . trim ( ) } (checkpoint)`
191+ : "Checkpoint branch" ,
192+ nextSessionId : forked . sessionId ,
193+ parentSessionKey : normalizeSqliteSessionKey ( operation . sourceKey ) ,
194+ totalTokens : forked . totalTokens ,
195+ } )
196+ : cloneSqliteCheckpointSessionEntry ( {
197+ currentEntry,
198+ nextSessionId : forked . sessionId ,
199+ preserveCompactionCheckpoints : true ,
200+ totalTokens : forked . totalTokens ,
201+ } ) ;
202+ writeSessionEntry ( database , targetKey , nextEntry ) ;
220203 return {
221204 status : "created" ,
222- key : params . targetKey ,
223- checkpoint,
224- entry : nextEntry ,
225- } ;
226- }
227-
228- function restoreSqliteCompactionCheckpointSessionInTransaction (
229- database : OpenClawAgentDatabase ,
230- params : {
231- checkpointId : string ;
232- expectedState : SessionEntryExpectedState ;
233- legacySource ?: SqliteCompactionCheckpointLegacySource ;
234- resolved : ResolvedSqliteScope ;
235- sourceKey : string ;
236- targetKey : string ;
237- } ,
238- ) : SqliteCompactionCheckpointSessionMutationResult {
239- const currentEntry = readSessionEntryRow ( database , params . sourceKey ) ?. entry ;
240- if ( ! currentEntry ?. sessionId ) {
241- return { status : "missing-session" } ;
242- }
243- if (
244- currentEntry . sessionId !== params . expectedState . sessionId ||
245- currentEntry . lifecycleRevision !== params . expectedState . lifecycleRevision
246- ) {
247- return { status : "conflict" } ;
248- }
249- if ( currentEntry . modelSelectionLocked === true ) {
250- return { status : "model-selection-locked" } ;
251- }
252- const checkpoint = readSessionCompactionCheckpoint ( currentEntry , params . checkpointId ) ;
253- if ( ! checkpoint ) {
254- return { status : "missing-checkpoint" } ;
255- }
256- const restored = forkSqliteCheckpointTranscriptInTransaction ( database , params . resolved , {
257- checkpoint,
258- legacySource : params . legacySource ,
259- targetSessionKey : params . targetKey ,
260- } ) ;
261- if ( restored . status !== "created" ) {
262- return restored ;
263- }
264-
265- const nextEntry = cloneSqliteCheckpointSessionEntry ( {
266- currentEntry,
267- nextSessionId : restored . sessionId ,
268- preserveCompactionCheckpoints : true ,
269- totalTokens : restored . totalTokens ,
270- } ) ;
271- writeSessionEntry ( database , params . targetKey , nextEntry ) ;
272- return {
273- status : "created" ,
274- key : params . targetKey ,
205+ key : targetKey ,
275206 checkpoint,
276207 entry : nextEntry ,
277208 } ;
@@ -488,5 +419,3 @@ function isSessionTranscriptHeader(event: TranscriptEvent): boolean {
488419 ( event as { type ?: unknown } ) . type === "session" ,
489420 ) ;
490421}
491-
492- /** Records inbound session metadata without refreshing activity timestamps. */
0 commit comments