@@ -11,6 +11,7 @@ import {
1111import { resolveAgentDir , resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime" ;
1212import { createDedupeCache } from "openclaw/plugin-sdk/dedupe-runtime" ;
1313import { coerceErrorMessage } from "openclaw/plugin-sdk/error-runtime" ;
14+ import { createDeferred } from "openclaw/plugin-sdk/extension-shared" ;
1415import { KeyedAsyncQueue } from "openclaw/plugin-sdk/keyed-async-queue" ;
1516import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime" ;
1617import { isIncognitoSessionKey } from "../incognito-session.js" ;
@@ -87,10 +88,8 @@ function watchCodexNativeCompactionCompletion(params: {
8788 let compactionItemId : string | undefined ;
8889 let compactionItemCompleted = false ;
8990 let tokensAfter : number | undefined ;
90- let resolveCompletion = ( _result : CodexNativeCompactionCompletion ) => { } ;
91- const completion = new Promise < CodexNativeCompactionCompletion > ( ( resolve ) => {
92- resolveCompletion = resolve ;
93- } ) ;
91+ const { promise : completion , resolve : resolveCompletion } =
92+ createDeferred < CodexNativeCompactionCompletion > ( ) ;
9493 let removeNotificationHandler = ( ) => { } ;
9594 let removeCloseHandler = ( ) => { } ;
9695 let removeAbortHandler = ( ) => { } ;
@@ -110,6 +109,7 @@ function watchCodexNativeCompactionCompletion(params: {
110109 } ;
111110 const complete = ( ) =>
112111 finish ( { completed : true , ...( tokensAfter !== undefined ? { tokensAfter } : { } ) } ) ;
112+ const fail = ( reason : string ) => finish ( { completed : false , reason } ) ;
113113 const retireUnconfirmed = ( reason : string ) => {
114114 if ( settled || retirementStarted ) {
115115 return ;
@@ -118,7 +118,7 @@ function watchCodexNativeCompactionCompletion(params: {
118118 // Timers started under the short-lived binding lease inherit its async
119119 // owner. Remote retirement must not reuse that already-released token.
120120 void runOutsideBindingLease ( ( ) => params . retireUnconfirmed ( ) )
121- . then ( ( ) => finish ( { completed : false , reason } ) )
121+ . then ( ( ) => fail ( reason ) )
122122 . catch ( ( error : unknown ) => {
123123 embeddedAgentLog . error ( "failed to retire unconfirmed codex app-server compaction" , {
124124 threadId : params . threadId ,
@@ -150,11 +150,9 @@ function watchCodexNativeCompactionCompletion(params: {
150150 complete ( ) ;
151151 return ;
152152 }
153- finish ( {
154- completed : false ,
155- reason :
156- "codex app-server compaction reached terminal state without a completed compaction item" ,
157- } ) ;
153+ fail (
154+ "codex app-server compaction reached terminal state without a completed compaction item" ,
155+ ) ;
158156 return ;
159157 }
160158 embeddedAgentLog . warn ( "codex app-server compaction interrupt request failed" , {
@@ -251,10 +249,7 @@ function watchCodexNativeCompactionCompletion(params: {
251249 const turn = isJsonObject ( notification . params . turn ) ? notification . params . turn : undefined ;
252250 const status = typeof turn ?. status === "string" ? turn . status : undefined ;
253251 if ( status !== "completed" ) {
254- finish ( {
255- completed : false ,
256- reason : `codex app-server compaction turn ended with status ${ status ?? "unknown" } ` ,
257- } ) ;
252+ fail ( `codex app-server compaction turn ended with status ${ status ?? "unknown" } ` ) ;
258253 return ;
259254 }
260255 const incompleteReason = ! compactionItemId
@@ -263,7 +258,7 @@ function watchCodexNativeCompactionCompletion(params: {
263258 ? "codex app-server compaction turn completed before its compaction item"
264259 : undefined ;
265260 if ( incompleteReason ) {
266- finish ( { completed : false , reason : incompleteReason } ) ;
261+ fail ( incompleteReason ) ;
267262 return ;
268263 }
269264 complete ( ) ;
@@ -291,15 +286,14 @@ function watchCodexNativeCompactionCompletion(params: {
291286 beginInterruptGrace ( ) ;
292287 }
293288 } ,
294- confirmRequestRejected : ( ) =>
295- finish ( { completed : false , reason : "codex app-server rejected the compaction request" } ) ,
289+ confirmRequestRejected : ( ) => fail ( "codex app-server rejected the compaction request" ) ,
296290 retireUnconfirmedRequest : async ( reason : string ) => {
297291 retireUnconfirmed ( reason ) ;
298292 return await completion ;
299293 } ,
300294 cancel : ( ) => {
301295 if ( ! requestStarted ) {
302- finish ( { completed : false , reason : "compaction request did not start" } ) ;
296+ fail ( "compaction request did not start" ) ;
303297 }
304298 } ,
305299 } ;
0 commit comments