@@ -15,6 +15,7 @@ import type {
1515 ReasoningEffortOption ,
1616 Thread ,
1717 ThreadItem ,
18+ TurnCompletedNotification ,
1819 UserInput
1920} from "./app-server/v2" ;
2021import type { RateLimitsMap } from "./RateLimitsMap" ;
@@ -1279,6 +1280,30 @@ export class CodexAcpServer {
12791280 return turnId ;
12801281 }
12811282
1283+ private async awaitCurrentCommandTurnCompletion (
1284+ sessionState : SessionState ,
1285+ activePrompt : ActivePrompt ,
1286+ ) : Promise < TurnCompletedNotification | null | undefined > {
1287+ const turnId = sessionState . currentTurnId ;
1288+ if ( ! turnId ) {
1289+ return undefined ;
1290+ }
1291+
1292+ const turnCompleted = await Promise . race ( [
1293+ this . runWithProcessCheck ( ( ) => this . codexAcpClient . awaitTurnCompleted ( {
1294+ threadId : sessionState . sessionId ,
1295+ turnId,
1296+ } ) ) ,
1297+ activePrompt . closeSignal ,
1298+ ] ) ;
1299+ if ( turnCompleted === null ) {
1300+ return null ;
1301+ }
1302+
1303+ await this . codexAcpClient . waitForSessionNotifications ( sessionState . sessionId ) ;
1304+ return turnCompleted ;
1305+ }
1306+
12821307 async prompt ( params : acp . PromptRequest ) : Promise < acp . PromptResponse > {
12831308 logger . log ( "Prompt received" , {
12841309 sessionId : params . sessionId ,
@@ -1306,7 +1331,18 @@ export class CodexAcpServer {
13061331 if ( commandResult . handled ) {
13071332 logger . log ( "Prompt handled by a command" ) ;
13081333 await this . codexAcpClient . waitForSessionNotifications ( params . sessionId ) ;
1309- if ( commandResult . turnCompleted ?. turn . status === "interrupted" ) {
1334+ let turnCompleted : TurnCompletedNotification | null | undefined = commandResult . turnCompleted ;
1335+ if ( ! turnCompleted && commandResult . waitForCurrentTurnCompletion ) {
1336+ turnCompleted = await this . awaitCurrentCommandTurnCompletion ( sessionState , activePrompt ) ;
1337+ if ( turnCompleted === null ) {
1338+ return {
1339+ stopReason : "cancelled" ,
1340+ usage : this . buildPromptUsage ( sessionState . lastTokenUsage ) ,
1341+ _meta : this . buildQuotaMeta ( sessionState ) ,
1342+ } ;
1343+ }
1344+ }
1345+ if ( turnCompleted ?. turn . status === "interrupted" ) {
13101346 if ( ! this . sessionIsClosing ( params . sessionId ) && this . sessions . has ( params . sessionId ) ) {
13111347 await this . connection . notify ( acp . methods . client . session . update , {
13121348 sessionId : params . sessionId ,
0 commit comments