@@ -26,6 +26,10 @@ import {
2626 buildEmbeddedCompactionRuntimeContext ,
2727 resolveEmbeddedCompactionTarget ,
2828} from "./compaction-runtime-context.js" ;
29+ import {
30+ rotateTranscriptAfterCompaction ,
31+ shouldRotateCompactionTranscript ,
32+ } from "./compaction-successor-transcript.js" ;
2933import { runContextEngineMaintenance } from "./context-engine-maintenance.js" ;
3034import { resolveGlobalLane , resolveSessionLane } from "./lanes.js" ;
3135import { log } from "./logger.js" ;
@@ -158,15 +162,44 @@ export async function compactEmbeddedPiSession(
158162 force : params . trigger === "manual" ,
159163 runtimeContext,
160164 } ) ;
165+ const delegatedSessionId = result . result ?. sessionId ;
166+ const delegatedSessionFile = result . result ?. sessionFile ;
167+ const delegatedRotatedTranscript = Boolean ( delegatedSessionId || delegatedSessionFile ) ;
168+ let postCompactionSessionId = delegatedSessionId ?? params . sessionId ;
169+ let postCompactionSessionFile = delegatedSessionFile ?? params . sessionFile ;
170+ let postCompactionLeafId : string | undefined ;
161171 if ( result . ok && result . compacted ) {
172+ if ( shouldRotateCompactionTranscript ( params . config ) && ! delegatedRotatedTranscript ) {
173+ try {
174+ const rotation = await rotateTranscriptAfterCompaction ( {
175+ sessionManager : SessionManager . open ( params . sessionFile ) ,
176+ sessionFile : params . sessionFile ,
177+ } ) ;
178+ if ( rotation . rotated ) {
179+ postCompactionSessionId = rotation . sessionId ?? postCompactionSessionId ;
180+ postCompactionSessionFile = rotation . sessionFile ?? postCompactionSessionFile ;
181+ postCompactionLeafId = rotation . leafId ;
182+ log . info (
183+ `[compaction] rotated active transcript after context-engine compaction ` +
184+ `(sessionKey=${ params . sessionKey ?? params . sessionId } )` ,
185+ ) ;
186+ }
187+ } catch ( err ) {
188+ log . warn ( "failed to rotate compacted transcript" , {
189+ errorMessage : formatErrorMessage ( err ) ,
190+ } ) ;
191+ }
192+ }
162193 if ( params . config && params . sessionKey && checkpointSnapshot ) {
163194 try {
164- const postCompactionSession = SessionManager . open ( params . sessionFile ) ;
165- const postLeafId = postCompactionSession . getLeafId ( ) ?? undefined ;
195+ const postLeafId =
196+ postCompactionLeafId ??
197+ SessionManager . open ( postCompactionSessionFile ) . getLeafId ( ) ??
198+ undefined ;
166199 const storedCheckpoint = await persistSessionCompactionCheckpoint ( {
167200 cfg : params . config ,
168201 sessionKey : params . sessionKey ,
169- sessionId : params . sessionId ,
202+ sessionId : postCompactionSessionId ,
170203 reason : resolveSessionCompactionCheckpointReason ( {
171204 trigger : params . trigger ,
172205 } ) ,
@@ -175,7 +208,7 @@ export async function compactEmbeddedPiSession(
175208 firstKeptEntryId : result . result ?. firstKeptEntryId ,
176209 tokensBefore : result . result ?. tokensBefore ,
177210 tokensAfter : result . result ?. tokensAfter ,
178- postSessionFile : params . sessionFile ,
211+ postSessionFile : postCompactionSessionFile ,
179212 postLeafId,
180213 postEntryId : postLeafId ,
181214 } ) ;
@@ -188,9 +221,9 @@ export async function compactEmbeddedPiSession(
188221 }
189222 await runContextEngineMaintenance ( {
190223 contextEngine,
191- sessionId : params . sessionId ,
224+ sessionId : postCompactionSessionId ,
192225 sessionKey : params . sessionKey ,
193- sessionFile : params . sessionFile ,
226+ sessionFile : postCompactionSessionFile ,
194227 reason : "compaction" ,
195228 runtimeContext,
196229 } ) ;
@@ -199,7 +232,7 @@ export async function compactEmbeddedPiSession(
199232 await runPostCompactionSideEffects ( {
200233 config : params . config ,
201234 sessionKey : params . sessionKey ,
202- sessionFile : params . sessionFile ,
235+ sessionFile : postCompactionSessionFile ,
203236 } ) ;
204237 }
205238 if (
@@ -209,14 +242,18 @@ export async function compactEmbeddedPiSession(
209242 hookRunner . runAfterCompaction
210243 ) {
211244 try {
245+ const afterHookCtx = {
246+ ...hookCtx ,
247+ sessionId : postCompactionSessionId ,
248+ } ;
212249 await hookRunner . runAfterCompaction (
213250 {
214251 messageCount : - 1 ,
215252 compactedCount : - 1 ,
216253 tokenCount : result . result ?. tokensAfter ,
217- sessionFile : params . sessionFile ,
254+ sessionFile : postCompactionSessionFile ,
218255 } ,
219- hookCtx ,
256+ afterHookCtx ,
220257 ) ;
221258 } catch ( err ) {
222259 log . warn ( "after_compaction hook failed" , {
@@ -235,6 +272,12 @@ export async function compactEmbeddedPiSession(
235272 tokensBefore : result . result . tokensBefore ,
236273 tokensAfter : result . result . tokensAfter ,
237274 details : result . result . details ,
275+ ...( postCompactionSessionId !== params . sessionId
276+ ? { sessionId : postCompactionSessionId }
277+ : { } ) ,
278+ ...( postCompactionSessionFile !== params . sessionFile
279+ ? { sessionFile : postCompactionSessionFile }
280+ : { } ) ,
238281 }
239282 : undefined ,
240283 } ;
0 commit comments