@@ -12,6 +12,7 @@ import {
1212} from "openclaw/plugin-sdk/diagnostic-runtime" ;
1313import * as mediaStore from "openclaw/plugin-sdk/media-store" ;
1414import { describe , expect , it , vi } from "vitest" ;
15+ import * as approvalBridge from "./approval-bridge.js" ;
1516import { buildCodexAppServerPromptTimeoutOutcome } from "./attempt-results.js" ;
1617import type { EmbeddedRunAttemptResult } from "./attempt-terminal.js" ;
1718import { readAttemptTerminal } from "./attempt-terminal.test-helper.js" ;
@@ -1146,6 +1147,71 @@ describe("runCodexAppServerAttempt turn watches", () => {
11461147 } ) ;
11471148 } ) ;
11481149
1150+ it ( "keeps turn request activity active until command approval resolves" , async ( ) => {
1151+ const harness = createStartedThreadHarness ( ) ;
1152+ const approvalResponse = { decision : "accept" } as const ;
1153+ let resolveApproval ! : ( value : typeof approvalResponse ) => void ;
1154+ const approval = new Promise < typeof approvalResponse > ( ( resolve ) => {
1155+ resolveApproval = resolve ;
1156+ } ) ;
1157+ vi . spyOn ( approvalBridge , "handleCodexAppServerApprovalRequest" ) . mockImplementation (
1158+ async ( ) => await approval ,
1159+ ) ;
1160+ const params = makeTestParams ( { timeoutMs : 500 } ) ;
1161+ const onRunProgress = vi . fn ( ) ;
1162+ params . onRunProgress = onRunProgress ;
1163+
1164+ const run = runCodexAppServerAttempt ( params , {
1165+ turnCompletionIdleTimeoutMs : 1_000 ,
1166+ turnAssistantCompletionIdleTimeoutMs : 1_000 ,
1167+ turnTerminalIdleTimeoutMs : 1_000 ,
1168+ } ) ;
1169+ await harness . waitForMethod ( "turn/start" ) ;
1170+
1171+ const response = harness . handleServerRequest ( {
1172+ id : "request-pending-approval" ,
1173+ method : "item/commandExecution/requestApproval" ,
1174+ params : {
1175+ threadId : "thread-1" ,
1176+ turnId : "turn-1" ,
1177+ itemId : "command-1" ,
1178+ command : "echo approved" ,
1179+ cwd : "/workspace" ,
1180+ } ,
1181+ } ) ;
1182+ await vi . waitFor (
1183+ ( ) =>
1184+ expect ( onRunProgress ) . toHaveBeenCalledWith (
1185+ expect . objectContaining ( {
1186+ reason : "request:item/commandExecution/requestApproval:start" ,
1187+ } ) ,
1188+ ) ,
1189+ fastWait ,
1190+ ) ;
1191+ expect (
1192+ onRunProgress . mock . calls . some (
1193+ ( [ event ] ) =>
1194+ ( event as { reason ?: string } ) . reason ===
1195+ "request:item/commandExecution/requestApproval:response" ,
1196+ ) ,
1197+ ) . toBe ( false ) ;
1198+
1199+ resolveApproval ( approvalResponse ) ;
1200+ await expect ( response ) . resolves . toEqual ( approvalResponse ) ;
1201+ expect ( onRunProgress ) . toHaveBeenCalledWith (
1202+ expect . objectContaining ( {
1203+ reason : "request:item/commandExecution/requestApproval:response" ,
1204+ } ) ,
1205+ ) ;
1206+ await harness . completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
1207+
1208+ expect ( readAttemptTerminal ( await run ) ) . toMatchObject ( {
1209+ aborted : false ,
1210+ timedOut : false ,
1211+ promptError : null ,
1212+ } ) ;
1213+ } ) ;
1214+
11491215 it ( "keeps an eliciting MCP tool active past the completion timeout" , async ( ) => {
11501216 const harness = createStartedThreadHarness ( ) ;
11511217 const bridgedResponse = {
@@ -1225,16 +1291,11 @@ describe("runCodexAppServerAttempt turn watches", () => {
12251291 } ) ;
12261292 } ) ;
12271293
1228- it ( "counts pending secret user input requests as turn attempt progress" , async ( ) => {
1229- // `item/tool/requestUserInput` records attempt progress but never suppresses
1230- // the attempt watch while pending, so this proves the turn outlives the
1294+ it ( "keeps secret user input request activity active until the answer arrives" , async ( ) => {
1295+ // Pending user input must keep the active request open and outlive the
12311296 // deadline `turn:start` alone would have set. That needs
12321297 // preRequestIdleMs + pendingHoldMs > attemptIdleTimeoutMs > pendingHoldMs;
12331298 // both bounds are asserted below so load can never make it vacuous.
1234- // The two request resets in run-attempt-server-requests.ts land ~20ms apart
1235- // (`finally` runs at `return`, not when the bridge promise settles), so this
1236- // cannot attribute survival to `:start` over `:response` — it guards the
1237- // pair. Mutating either one alone still passes; mutating both fails.
12381299 const attemptIdleTimeoutMs = 1_000 ;
12391300 const preRequestIdleMs = 500 ;
12401301 const pendingHoldMs = 700 ;
@@ -1271,6 +1332,7 @@ describe("runCodexAppServerAttempt turn watches", () => {
12711332 threadId : "thread-1" ,
12721333 turnId : "turn-1" ,
12731334 itemId : "input-1" ,
1335+ isBlocking : true ,
12741336 questions : [
12751337 {
12761338 id : "mode" ,
@@ -1300,10 +1362,21 @@ describe("runCodexAppServerAttempt turn watches", () => {
13001362 // Lower bound: the turn has now outlived the deadline turn:start alone set,
13011363 // so surviving proves the pending request moved it.
13021364 expect ( Date . now ( ) - turnObservedAt ) . toBeGreaterThan ( attemptIdleTimeoutMs ) ;
1303- expect ( queueActiveRunMessageForTest ( "session-1" , "2" ) ) . toBe ( true ) ;
1365+ expect (
1366+ onRunProgress . mock . calls . some (
1367+ ( [ event ] ) =>
1368+ ( event as { reason ?: string } ) . reason === "request:item/tool/requestUserInput:response" ,
1369+ ) ,
1370+ ) . toBe ( false ) ;
1371+ expect ( queueActiveRunMessageForTest ( "session-1" , "2" , { isInboundUserMessage : true } ) ) . toBe (
1372+ true ,
1373+ ) ;
13041374 await expect ( response ) . resolves . toEqual ( {
13051375 answers : { mode : { answers : [ "Deep" ] } } ,
13061376 } ) ;
1377+ expect ( onRunProgress ) . toHaveBeenCalledWith (
1378+ expect . objectContaining ( { reason : "request:item/tool/requestUserInput:response" } ) ,
1379+ ) ;
13071380 await harness . completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
13081381
13091382 const result = await run ;
0 commit comments