@@ -3987,6 +3987,126 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39873987 }
39883988 }
39893989
3990+ // The app-server exits after the root turn, before the command's own
3991+ // item/completed (Codex always sends one, so only a lost notification or a
3992+ // gone process leaves it running). Nothing tracks the command any more, yet
3993+ // the thread still shows it, and Stop is the only way to clear it.
3994+ it . effect ( "Stop ends a background command no Codex process tracks any more" , ( ) =>
3995+ Effect . scoped (
3996+ Effect . gen ( function * ( ) {
3997+ const fs = yield * FileSystem . FileSystem ;
3998+ const cwd = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-bg-stale-workspace-" } ) ;
3999+ const staleTranscript = makeCodexReplayTranscript ( {
4000+ scenario : "codex-bg-stop-untracked" ,
4001+ entries : [
4002+ ...backgroundExecTranscript . entries . slice ( 0 , - 1 ) ,
4003+ { type : "runtime_exit" , status : "success" } ,
4004+ ] ,
4005+ } ) ;
4006+ const localTranscript = yield * decodeReplayTranscriptJson (
4007+ ( yield * encodeReplayTranscriptJson ( staleTranscript ) ) . replaceAll (
4008+ yield * encodeStringJson ( "/workspace" ) ,
4009+ yield * encodeStringJson ( cwd ) ,
4010+ ) ,
4011+ ) ;
4012+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
4013+ for ( const args of [
4014+ [ "init" , "--quiet" ] ,
4015+ [
4016+ "-c" ,
4017+ "user.name=Test" ,
4018+ "-c" ,
4019+ 4020+ "commit" ,
4021+ "--allow-empty" ,
4022+ "--quiet" ,
4023+ "-m" ,
4024+ "Initial commit" ,
4025+ ] ,
4026+ ] ) {
4027+ assert . equal ( Number ( yield * spawner . exitCode ( ChildProcess . make ( "git" , args , { cwd } ) ) ) , 0 ) ;
4028+ }
4029+ yield * Effect . gen ( function * ( ) {
4030+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
4031+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
4032+ const threadId = ThreadId . make ( "thread:background-stop-untracked" ) ;
4033+ yield * orchestrator . dispatch ( {
4034+ type : "thread.create" ,
4035+ commandId : CommandId . make ( "create-background-stop-untracked" ) ,
4036+ threadId,
4037+ projectId : ProjectId . make ( "project:background-stop-untracked" ) ,
4038+ title : "Background stop untracked" ,
4039+ modelSelection : CODEX_TEST_MODEL_SELECTION ,
4040+ runtimeMode : "full-access" ,
4041+ interactionMode : "default" ,
4042+ branch : null ,
4043+ worktreePath : cwd ,
4044+ createdBy : "user" ,
4045+ creationSource : "web" ,
4046+ } ) ;
4047+ const settled = yield * orchestrator . streamDomainEvents . pipe (
4048+ Stream . filter (
4049+ ( event ) =>
4050+ event . type === "run.updated" &&
4051+ ( event . payload . status === "waiting" || event . payload . status === "completed" ) ,
4052+ ) ,
4053+ Stream . runHead ,
4054+ Effect . forkChild ( { startImmediately : true } ) ,
4055+ ) ;
4056+ yield * orchestrator . dispatch ( {
4057+ type : "message.dispatch" ,
4058+ commandId : CommandId . make ( "start-background-stop-untracked" ) ,
4059+ threadId,
4060+ messageId : MessageId . make ( "message:background-stop-untracked" ) ,
4061+ text : BG_PROMPT ,
4062+ attachments : [ ] ,
4063+ createdBy : "user" ,
4064+ creationSource : "web" ,
4065+ dispatchMode : { type : "start_immediately" } ,
4066+ } ) ;
4067+ yield * worker . drain ( ) ;
4068+ yield * Fiber . join ( settled ) ;
4069+ yield * worker . drain ( ) ;
4070+ const before = yield * orchestrator . getThreadShell ( threadId ) ;
4071+ assert . deepEqual (
4072+ before ?. pendingBackgroundTasks ?. map ( ( task ) => task . kind ) ,
4073+ [ "command" ] ,
4074+ "the thread still shows the command the gone process never finished" ,
4075+ ) ;
4076+ const run = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . at ( - 1 ) ! ;
4077+ yield * orchestrator . dispatch ( {
4078+ type : "run.interrupt" ,
4079+ commandId : CommandId . make ( "stop-background-untracked" ) ,
4080+ threadId,
4081+ runId : run . id ,
4082+ holdQueue : true ,
4083+ } ) ;
4084+ yield * worker . drain ( ) ;
4085+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
4086+ assert . deepEqual (
4087+ projection . turnItems . flatMap ( ( item ) =>
4088+ item . type === "command_execution" ? [ item . status ] : [ ] ,
4089+ ) ,
4090+ [ "interrupted" ] ,
4091+ ) ;
4092+ assert . equal ( projection . runs . at ( - 1 ) ?. status , "completed" ) ;
4093+ assert . deepEqual (
4094+ ( yield * orchestrator . getThreadShell ( threadId ) ) ?. pendingBackgroundTasks ,
4095+ [ ] ,
4096+ ) ;
4097+ } ) . pipe (
4098+ Effect . provide (
4099+ makeOrchestratorV2ReplayLayerWithRegistry (
4100+ { name : "codex-background-stop-untracked" , runtimePolicyOverride : { cwd } } ,
4101+ makeCodexProviderAdapterRegistryReplayLayer ( { transcript : localTranscript } ) ,
4102+ { runEffectWorker : false } ,
4103+ ) ,
4104+ ) ,
4105+ ) ;
4106+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
4107+ ) ,
4108+ ) ;
4109+
39904110 const PRE_SETTLE_SCENARIO = "codex-bg-exec-pre-settle" ;
39914111 const PRE_SETTLE_NATIVE_THREAD = "native-codex-pre-settle-thread" ;
39924112 const PRE_SETTLE_NATIVE_TURN = "native-codex-pre-settle-turn" ;
0 commit comments