@@ -21,6 +21,11 @@ import {ModelId} from "../../ModelId";
2121import { GOAL_CONTROL_METHOD } from "../../AcpExtensions" ;
2222import { ClientCapabilities } from "../../tool-calls/ClientCapabilities" ;
2323import type { McpStartupResult } from "../../mcp/McpStartupTracker" ;
24+ import {
25+ createRecoveryFixture ,
26+ initialize as initializeRecoveryFixture ,
27+ requestsOf as requestsOfRecoveryServer ,
28+ } from "../app-server-recovery/recovery-fixture" ;
2429
2530describe ( 'ACP server test' , { timeout : 40_000 } , ( ) => {
2631
@@ -3441,6 +3446,110 @@ describe('ACP server test', { timeout: 40_000 }, () => {
34413446 expect ( turnStartSpy ) . not . toHaveBeenCalled ( ) ;
34423447 } ) ;
34433448
3449+ it ( 'forwards goal work set before the first prompt' , async ( ) => {
3450+ const { mockFixture, sessionState} = setupPromptFixture ( ) ;
3451+ // @ts -expect-error - registering local session state for the extension request path
3452+ mockFixture . getCodexAcpAgent ( ) . sessions . set ( "session-id" , sessionState ) ;
3453+ const goal = createThreadGoal ( { objective : "Finish the migration" , status : "active" } ) ;
3454+ vi . spyOn ( mockFixture . getCodexAcpClient ( ) , "setGoal" )
3455+ . mockImplementation ( async ( _sessionId , _objective , _onTurnStarted , onGoalSet ) => {
3456+ mockFixture . sendServerNotification ( {
3457+ method : "thread/goal/updated" ,
3458+ params : { threadId : "session-id" , turnId : null , goal} ,
3459+ } ) ;
3460+ onGoalSet ?.( goal ) ;
3461+ mockFixture . sendServerNotification ( {
3462+ method : "item/agentMessage/delta" ,
3463+ params : { threadId : "session-id" , turnId : "routed-goal-turn" , itemId : "message" , delta : "Working on it" } ,
3464+ } ) ;
3465+ return {
3466+ threadId : "session-id" ,
3467+ turn : createTurn ( "routed-goal-turn" , "completed" ) ,
3468+ } ;
3469+ } ) ;
3470+ mockFixture . clearAcpConnectionDump ( ) ;
3471+
3472+ await expect ( mockFixture . getCodexAcpAgent ( ) . extMethod ( GOAL_CONTROL_METHOD , {
3473+ sessionId : "session-id" ,
3474+ action : "set" ,
3475+ objective : goal . objective ,
3476+ } ) ) . resolves . toEqual ( { } ) ;
3477+ await mockFixture . getCodexAcpClient ( ) . waitForSessionNotifications ( "session-id" ) ;
3478+
3479+ const updates = mockFixture . getAcpConnectionEvents ( [ ] )
3480+ . filter ( event => event . method === "sessionUpdate" && "args" in event )
3481+ . map ( event => event . args [ 0 ] ?. update ) ;
3482+ expect ( updates ) . toEqual ( expect . arrayContaining ( [
3483+ expect . objectContaining ( {
3484+ sessionUpdate : "session_info_update" ,
3485+ _meta : { jetbrains : { air : { version : 1 , goal : expect . objectContaining ( { objective : goal . objective } ) } } } ,
3486+ } ) ,
3487+ expect . objectContaining ( {
3488+ sessionUpdate : "agent_message_chunk" ,
3489+ content : expect . objectContaining ( { text : "Working on it" } ) ,
3490+ } ) ,
3491+ ] ) ) ;
3492+ } ) ;
3493+
3494+ it ( 'forwards goal work set before the first prompt again after the app-server restarted' , async ( ) => {
3495+ const fixture = createRecoveryFixture ( ) ;
3496+ await initializeRecoveryFixture ( fixture , true ) ;
3497+ const { sessionId} = await fixture . agent . newSession ( { cwd : "/work" , mcpServers : [ ] } ) ;
3498+ const goal = { threadId : sessionId , objective : "ship it" , status : "paused" , tokenBudget : null , tokensUsed : 0 , timeUsedSeconds : 0 , createdAt : 1 , updatedAt : 2 } ;
3499+ fixture . answers . set ( "thread/goal/set" , ( _params , server ) => {
3500+ setImmediate ( ( ) => server . rpc . notify ( { method : "thread/goal/updated" , params : { threadId : sessionId , turnId : null , goal} } ) ) ;
3501+ return { goal} ;
3502+ } ) ;
3503+ const agentMessages = ( ) => fixture . updates ( )
3504+ . filter ( ( { sessionId : id , update} ) => id === sessionId && update [ "sessionUpdate" ] === "agent_message_chunk" )
3505+ . map ( ( { update} ) => ( update [ "content" ] as { text ?: string } ) . text ) ;
3506+ const goalWork = async ( delta : string ) => {
3507+ await fixture . agent . extMethod ( GOAL_CONTROL_METHOD , { sessionId, action : "pause" } ) ;
3508+ fixture . current ( ) . rpc . notify ( {
3509+ method : "item/agentMessage/delta" ,
3510+ params : { threadId : sessionId , turnId : "goal-turn" , itemId : `message-${ delta } ` , delta} ,
3511+ } ) ;
3512+ await vi . waitFor ( ( ) => expect ( agentMessages ( ) ) . toContain ( delta ) ) ;
3513+ } ;
3514+
3515+ await goalWork ( "before the restart" ) ;
3516+ await fixture . kill ( ) ;
3517+ await goalWork ( "after the restart" ) ;
3518+
3519+ expect ( fixture . servers ) . toHaveLength ( 2 ) ;
3520+ expect ( requestsOfRecoveryServer ( fixture . current ( ) , "thread/resume" ) ) . toHaveLength ( 1 ) ;
3521+ } ) ;
3522+
3523+ it ( 'cancels a permission request of goal work set before the first prompt when its app-server dies' , async ( ) => {
3524+ const fixture = createRecoveryFixture ( ) ;
3525+ await initializeRecoveryFixture ( fixture , true ) ;
3526+ const { sessionId} = await fixture . agent . newSession ( { cwd : "/work" , mcpServers : [ ] } ) ;
3527+ const goal = { threadId : sessionId , objective : "ship it" , status : "paused" , tokenBudget : null , tokensUsed : 0 , timeUsedSeconds : 0 , createdAt : 1 , updatedAt : 2 } ;
3528+ fixture . answers . set ( "thread/goal/set" , ( _params , server ) => {
3529+ setImmediate ( ( ) => server . rpc . notify ( { method : "thread/goal/updated" , params : { threadId : sessionId , turnId : null , goal} } ) ) ;
3530+ return { goal} ;
3531+ } ) ;
3532+ let permissionSignal : AbortSignal | undefined ;
3533+ fixture . acp . request . mockImplementation ( async ( method : string , _params : unknown , options ?: { cancellationSignal ?: AbortSignal } ) => {
3534+ if ( method === "session/request_permission" ) {
3535+ permissionSignal = options ?. cancellationSignal ;
3536+ return await new Promise ( ( ) => { } ) ;
3537+ }
3538+ return { } ;
3539+ } ) ;
3540+ await fixture . agent . extMethod ( GOAL_CONTROL_METHOD , { sessionId, action : "pause" } ) ;
3541+ const approval = fixture . current ( ) . rpc . requestHandlers . get ( "item/commandExecution/requestApproval" ) ! ( {
3542+ threadId : sessionId , turnId : "goal-turn" , itemId : "cmd-1" , command : "rm -rf build" , cwd : "/work" , reason : null ,
3543+ availableDecisions : [ "accept" , "decline" , "cancel" ] ,
3544+ } ) ;
3545+ await vi . waitFor ( ( ) => expect ( permissionSignal ) . toBeDefined ( ) ) ;
3546+
3547+ await fixture . kill ( ) ;
3548+
3549+ expect ( permissionSignal ! . aborted ) . toBe ( true ) ;
3550+ await expect ( approval ) . resolves . toEqual ( { decision : "cancel" } ) ;
3551+ } ) ;
3552+
34443553 it ( 'ignores an older goal refresh that completes after a newer refresh' , async ( ) => {
34453554 const mockFixture = createCodexMockTestFixture ( ) ;
34463555 const codexAcpAgent = mockFixture . getCodexAcpAgent ( ) ;
0 commit comments