@@ -46,6 +46,7 @@ export class FullCompaction {
4646 startedAt : number ;
4747 telemetryTrigger : CompactionTelemetryTrigger ;
4848 promise : Promise < void > ;
49+ blockedByTurn : boolean ;
4950 } | null = null ;
5051 protected _compactedHistory : CompactedHistory [ ] = [ ] ;
5152 protected readonly strategy : CompactionStrategy ;
@@ -112,6 +113,7 @@ export class FullCompaction {
112113 startedAt : Date . now ( ) ,
113114 telemetryTrigger : compactionTelemetryTrigger ( data . source , data . instruction ) ,
114115 promise : Promise . resolve ( ) ,
116+ blockedByTurn : false ,
115117 } ;
116118 this . compacting = active ;
117119 active . promise = this . compactionWorker ( abortController . signal , data , compactedCount ) ;
@@ -195,6 +197,7 @@ export class FullCompaction {
195197 private async block ( signal : AbortSignal ) : Promise < void > {
196198 const active = this . compacting ;
197199 if ( active ) {
200+ active . blockedByTurn = true ;
198201 signal . addEventListener ( 'abort' , ( ) => {
199202 if ( this . compacting === active ) {
200203 this . cancel ( ) ;
@@ -312,26 +315,34 @@ export class FullCompaction {
312315 this . triggerPostCompactHook ( data , result ) ;
313316 } catch ( error ) {
314317 if ( ! isAbortError ( error ) ) {
318+ const active = this . compacting ;
319+ const blockedByTurn = active ?. blockedByTurn === true ;
315320 this . agent . log . error ( 'compaction failed' , {
316321 code : isKimiError ( error ) ? error . code : undefined ,
317322 error,
318323 } ) ;
319324 this . markCanceled ( ) ;
320- const payload =
321- isKimiError ( error ) && error . code === ErrorCodes . AUTH_LOGIN_REQUIRED
322- ? toKimiErrorPayload ( error )
323- : makeErrorPayload ( ErrorCodes . COMPACTION_FAILED , String ( error ) ) ;
324- this . agent . emitEvent ( {
325- type : 'error' ,
326- ...payload ,
327- } ) ;
325+ if ( ! blockedByTurn ) {
326+ const payload =
327+ isKimiError ( error ) && error . code === ErrorCodes . AUTH_LOGIN_REQUIRED
328+ ? toKimiErrorPayload ( error )
329+ : makeErrorPayload ( ErrorCodes . COMPACTION_FAILED , String ( error ) ) ;
330+ this . agent . emitEvent ( {
331+ type : 'error' ,
332+ ...payload ,
333+ } ) ;
334+ }
328335 this . agent . telemetry . track ( 'compaction_failed' , {
329336 trigger_type : compactionTelemetryTrigger ( data . source , data . instruction ) ,
330337 before_tokens : tokensBefore ,
331338 duration_ms : Date . now ( ) - startedAt ,
332339 retry_count : retryCount ,
333340 error_type : error instanceof Error ? error . name : 'Unknown' ,
334341 } ) ;
342+ if ( blockedByTurn ) {
343+ if ( isKimiError ( error ) && error . code === ErrorCodes . AUTH_LOGIN_REQUIRED ) throw error ;
344+ throw new KimiError ( ErrorCodes . COMPACTION_FAILED , String ( error ) , { cause : error } ) ;
345+ }
335346 }
336347 }
337348 }
0 commit comments