@@ -30,10 +30,11 @@ export function persistSessionTranscriptArchive(
3030 plan : MaterializedSessionStateDeletePlan ,
3131) : void {
3232 const archive = plan . archive ;
33+ const generation = plan . snapshot . generation ;
3334 const sessionKey = plan . snapshot . sessionKey ;
34- if ( ! archive || ! sessionKey ) {
35+ if ( ! archive || ! generation || ! sessionKey ) {
3536 throw new Error (
36- `Cannot persist SQLite transcript archive without an owner for ${ plan . sessionId } ` ,
37+ `Cannot persist SQLite transcript archive without an owner generation for ${ plan . sessionId } ` ,
3738 ) ;
3839 }
3940 ensureSessionTranscriptArchiveSchema ( database . db ) ;
@@ -48,14 +49,15 @@ export function persistSessionTranscriptArchive(
4849 archive_sha256 : archive . sha256 ,
4950 created_at : archive . createdAt ,
5051 encoding : archive . encoding ,
52+ generation,
5153 last_publish_attempt_at : null ,
5254 last_publish_error : null ,
5355 published_at : null ,
5456 reason : plan . reason ,
5557 session_id : plan . sessionId ,
5658 session_key : sessionKey ,
5759 } )
58- . onConflict ( ( conflict ) => conflict . column ( "session_id" ) . doNothing ( ) ) ,
60+ . onConflict ( ( conflict ) => conflict . columns ( [ "session_id" , "generation" ] ) . doNothing ( ) ) ,
5961 ) ;
6062 const persisted = executeSqliteQueryTakeFirstSync (
6163 database . db ,
@@ -70,7 +72,8 @@ export function persistSessionTranscriptArchive(
7072 "reason" ,
7173 "session_key" ,
7274 ] )
73- . where ( "session_id" , "=" , plan . sessionId ) ,
75+ . where ( "session_id" , "=" , plan . sessionId )
76+ . where ( "generation" , "=" , generation ) ,
7477 ) ;
7578 if (
7679 ! persisted ||
@@ -88,19 +91,42 @@ export function persistSessionTranscriptArchive(
8891
8992const PENDING_ARCHIVE_PUBLISH_BATCH_SIZE = 4 ;
9093
94+ // Composite map keys keep repeated physical IDs distinct across transcript rewrites.
95+ function transcriptArchiveIdentityKey ( sessionId : string , generation : string ) : string {
96+ return `${ sessionId } \u0000${ generation } ` ;
97+ }
98+
99+ // Retain one publication plan per immutable archive identity.
100+ function uniqueTranscriptArchives < T extends { generation : string ; sessionId : string } > (
101+ archives : readonly T [ ] ,
102+ ) : T [ ] {
103+ return [
104+ ...new Map (
105+ archives . map ( ( archive ) => [
106+ transcriptArchiveIdentityKey ( archive . sessionId , archive . generation ) ,
107+ archive ,
108+ ] ) ,
109+ ) . values ( ) ,
110+ ] ;
111+ }
112+
91113/** Publishes derived archive files after their canonical rows and deletions commit. */
92114export async function publishSessionStateArchives (
93115 scope : Pick < ResolvedSqliteReadScope , "agentId" | "env" | "path" > ,
94116 requested : readonly SessionLifecycleArchivedTranscript [ ] ,
95117) : Promise < SessionLifecycleArchivedTranscript [ ] > {
96- const requestedIds = [ ...new Set ( requested . map ( ( archive ) => archive . sessionId ) ) ] ;
97- const requestedIdSet = new Set ( requestedIds ) ;
118+ const requestedArchives = uniqueTranscriptArchives ( requested ) ;
119+ const requestedIdentitySet = new Set (
120+ requestedArchives . map ( ( archive ) =>
121+ transcriptArchiveIdentityKey ( archive . sessionId , archive . generation ) ,
122+ ) ,
123+ ) ;
98124 let includeRequested = true ;
99125 while ( true ) {
100126 const plans = await runExclusiveSqliteSessionWrite ( scope , async ( ) => {
101127 const database = openOpenClawAgentDatabase ( toDatabaseOptions ( scope ) ) ;
102128 const db = getSessionKysely ( database . db ) ;
103- if ( includeRequested && requestedIds . length > 0 ) {
129+ if ( includeRequested && requestedArchives . length > 0 ) {
104130 ensureSessionTranscriptArchiveSchema ( database . db ) ;
105131 } else {
106132 const exists = executeSqliteQueryTakeFirstSync (
@@ -115,23 +141,28 @@ export async function publishSessionStateArchives(
115141 return [ ] ;
116142 }
117143 }
118- const pendingIds = executeSqliteQuerySync (
144+ const pendingArchives = executeSqliteQuerySync (
119145 database . db ,
120146 db
121147 . selectFrom ( "session_transcript_archives" )
122- . select ( " session_id")
148+ . select ( [ "generation" , " session_id"] )
123149 . where ( "published_at" , "is" , null )
124150 . orderBy ( "created_at" , "asc" )
125151 . orderBy ( "session_id" , "asc" )
152+ . orderBy ( "generation" , "asc" )
126153 . limit ( PENDING_ARCHIVE_PUBLISH_BATCH_SIZE ) ,
127- ) . rows . map ( ( row ) => row . session_id ) ;
128- const sessionIds = [ ...new Set ( [ ...( includeRequested ? requestedIds : [ ] ) , ...pendingIds ] ) ] ;
154+ ) . rows . map ( ( row ) => ( { generation : row . generation , sessionId : row . session_id } ) ) ;
155+ const archives = uniqueTranscriptArchives ( [
156+ ...( includeRequested ? requestedArchives : [ ] ) ,
157+ ...pendingArchives ,
158+ ] ) ;
129159 const archiveDirectory = resolveSqliteTranscriptArchiveDirectory ( scope ) ;
130- return sessionIds . map ( ( sessionId ) => ( {
160+ return archives . map ( ( archive ) => ( {
131161 agentId : database . agentId ,
132162 archiveDirectory,
133163 databasePath : database . path ,
134- sessionId,
164+ generation : archive . generation ,
165+ sessionId : archive . sessionId ,
135166 } ) ) ;
136167 } ) ;
137168 includeRequested = false ;
@@ -156,23 +187,28 @@ export async function publishSessionStateArchives(
156187 publish_attempts : eb ( "publish_attempts" , "+" , 1 ) ,
157188 ...( result . archivedPath ? { published_at : now } : { } ) ,
158189 } ) )
159- . where ( "session_id" , "=" , result . sessionId ) ,
190+ . where ( "session_id" , "=" , result . sessionId )
191+ . where ( "generation" , "=" , result . generation ) ,
160192 ) ;
161193 }
162194 } , toDatabaseOptions ( scope ) ) ;
163195 } ) ;
164196
165- const planBySessionId = new Map ( plans . map ( ( plan ) => [ plan . sessionId , plan ] ) ) ;
197+ const planByIdentity = new Map (
198+ plans . map ( ( plan ) => [ transcriptArchiveIdentityKey ( plan . sessionId , plan . generation ) , plan ] ) ,
199+ ) ;
166200 emitArchivedTranscriptUpdates (
167201 results . flatMap ( ( result ) => {
168- if ( ! result . archivedPath || requestedIdSet . has ( result . sessionId ) ) {
202+ const identity = transcriptArchiveIdentityKey ( result . sessionId , result . generation ) ;
203+ if ( ! result . archivedPath || requestedIdentitySet . has ( identity ) ) {
169204 return [ ] ;
170205 }
171- const plan = planBySessionId . get ( result . sessionId ) ;
206+ const plan = planByIdentity . get ( identity ) ;
172207 return plan
173208 ? [
174209 {
175210 archivedPath : result . archivedPath ,
211+ generation : result . generation ,
176212 sessionId : result . sessionId ,
177213 sourcePath : path . join ( plan . archiveDirectory , `${ result . sessionId } .jsonl` ) ,
178214 } ,
@@ -224,10 +260,18 @@ export async function prunePublishedSessionArchivesByRetention(params: {
224260 database . db ,
225261 db
226262 . selectFrom ( "session_transcript_archives" )
227- . select ( [ "archive_name" , "created_at" , "published_at" , "reason" , "session_id" ] )
263+ . select ( [
264+ "archive_name" ,
265+ "created_at" ,
266+ "generation" ,
267+ "published_at" ,
268+ "reason" ,
269+ "session_id" ,
270+ ] )
228271 . where ( "published_at" , "is not" , null )
229272 . orderBy ( "created_at" , "asc" )
230273 . orderBy ( "session_id" , "asc" )
274+ . orderBy ( "generation" , "asc" )
231275 . limit ( ARCHIVE_RETENTION_BATCH_SIZE ) ,
232276 ) . rows ;
233277 } ) ;
@@ -258,6 +302,7 @@ export async function prunePublishedSessionArchivesByRetention(params: {
258302 db
259303 . deleteFrom ( "session_transcript_archives" )
260304 . where ( "session_id" , "=" , row . session_id )
305+ . where ( "generation" , "=" , row . generation )
261306 . where ( "archive_name" , "=" , row . archive_name )
262307 . where ( "created_at" , "=" , row . created_at )
263308 . where ( "published_at" , "=" , row . published_at ) ,
0 commit comments