@@ -2045,6 +2045,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
20452045 const goalActivations = new Map < string , { stopped : boolean } > ( ) ;
20462046 // Held runs must not complete while Stop resolves its target.
20472047 const goalStops = new Map < string , number > ( ) ;
2048+ // Goals paused because a delegated task will wake the thread; the next ordinary turn resumes them.
2049+ const goalsPausedForWake = new Set < string > ( ) ;
20482050 const latestRunTurnId = ( providerTurnId : ProviderTurnId ) =>
20492051 continuedRuns . get ( providerTurnId ) ?. at ( - 1 ) ?. providerTurnId ?? providerTurnId ;
20502052 const isContinuedRunDescendant = (
@@ -6279,14 +6281,31 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
62796281 event . status === "completed" &&
62806282 nativeThreadId != null
62816283 ) {
6284+ const hasPendingWake = ( yield * mcpSessions . read ( input . context . input . threadId ) )
6285+ ?. hasPendingWake ;
6286+ // Decided under the permit before the hold exists, so no continuation can adopt it first.
6287+ const waitsForWake = hasPendingWake === undefined ? false : yield * hasPendingWake ;
62826288 goalHolds . set ( nativeThreadId , {
62836289 context : input . context ,
62846290 event,
62856291 completedTurn : input . goalHoldTurn ,
62866292 next : yield * Deferred . make < ActiveCodexTurnContext | undefined > ( ) ,
62876293 } ) ;
6294+ // A continuation turn would only report that the delegated task is still running.
6295+ const claimed = waitsForWake ? claimGoalHold ( nativeThreadId , event ) : undefined ;
6296+ if ( claimed !== undefined ) {
6297+ yield * pauseClaimedGoal ( nativeThreadId , claimed , true ) . pipe ( Effect . forkIn ( scope ) ) ;
6298+ return ;
6299+ }
62886300 yield * Effect . sleep ( CODEX_GOAL_CONTINUATION_GRACE ) . pipe (
6289- Effect . andThen ( pauseUncontinuedGoal ( nativeThreadId , event ) ) ,
6301+ Effect . andThen (
6302+ Effect . suspend ( ( ) => {
6303+ const hold = claimGoalHold ( nativeThreadId , event ) ;
6304+ return hold === undefined
6305+ ? Effect . void
6306+ : pauseClaimedGoal ( nativeThreadId , hold , false ) ;
6307+ } ) ,
6308+ ) ,
62906309 Effect . andThen (
62916310 turnTerminalizationPermit . withPermits ( 1 ) ( releaseGoalHold ( nativeThreadId , event ) ) ,
62926311 ) ,
@@ -6301,31 +6320,40 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
63016320
63026321 // A goal must not read as active once its run ends. Claiming the hold first
63036322 // makes a turn Codex starts during the pause an orphan, which is stopped.
6304- const pauseUncontinuedGoal = Effect . fnUntraced ( function * (
6305- nativeThreadId : string ,
6306- expected : CodexRootTerminalEvent ,
6307- ) {
6323+ const claimGoalHold = ( nativeThreadId : string , expected : CodexRootTerminalEvent ) => {
63086324 const hold = goalHolds . get ( nativeThreadId ) ;
63096325 if (
63106326 hold ?. event !== expected ||
63116327 goalStops . has ( nativeThreadId ) ||
63126328 goalsByNativeThread . get ( nativeThreadId ) ?. status !== "active"
63136329 ) {
6314- return ;
6330+ return undefined ;
63156331 }
63166332 goalHolds . delete ( nativeThreadId ) ;
63176333 // Keeps a turn Codex starts before settlement an orphan once the goal reads paused.
63186334 goalStops . set ( nativeThreadId , ( goalStops . get ( nativeThreadId ) ?? 0 ) + 1 ) ;
6319- yield * Effect . logWarning ( "orchestration-v2.codex-goal-continuation-expired" , {
6320- nativeThreadId,
6321- } ) ;
6335+ return hold ;
6336+ } ;
6337+
6338+ const pauseClaimedGoal = Effect . fnUntraced ( function * (
6339+ nativeThreadId : string ,
6340+ hold : CodexGoalHold ,
6341+ waitsForWake : boolean ,
6342+ ) {
6343+ if ( ! waitsForWake ) {
6344+ yield * Effect . logWarning ( "orchestration-v2.codex-goal-continuation-expired" , {
6345+ nativeThreadId,
6346+ } ) ;
6347+ }
63226348 yield * client
63236349 . request ( "thread/goal/set" , { threadId : nativeThreadId , status : "paused" } )
63246350 . pipe (
63256351 // The hold is already claimed, so an unanswered pause must not keep the run open.
63266352 Effect . timeout ( "10 seconds" ) ,
63276353 Effect . flatMap ( ( { goal } ) => {
63286354 goalsByNativeThread . set ( nativeThreadId , providerGoalFromCodex ( goal ) ) ;
6355+ if ( waitsForWake && goal . status === "paused" )
6356+ goalsPausedForWake . add ( nativeThreadId ) ;
63296357 return emitGoalUpdate ( nativeThreadId ) ;
63306358 } ) ,
63316359 Effect . catch ( ( cause ) =>
@@ -7275,6 +7303,47 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
72757303 ) ,
72767304 ) ;
72777305
7306+ /** Starts a turn and then activates its paused goal; Stop cancels activation. */
7307+ const activateGoal = Effect . fnUntraced ( function * (
7308+ turnInput : ProviderAdapter . ProviderAdapterV2TurnInput ,
7309+ threadId : string ,
7310+ codexInput : ReturnType < typeof toCodexInput > ,
7311+ ) {
7312+ goalsPausedForWake . delete ( threadId ) ;
7313+ // Hold fast first turns until activation.
7314+ const activation = { stopped : false } ;
7315+ goalActivations . set ( threadId , activation ) ;
7316+ yield * Effect . gen ( function * ( ) {
7317+ yield * startNativeTurn ( turnInput , yield * codexInput ) ;
7318+ if ( activation . stopped ) return ;
7319+ const { goal } = yield * client . request ( "thread/goal/set" , {
7320+ threadId,
7321+ status : "active" ,
7322+ } ) ;
7323+ goalsByNativeThread . set ( threadId , providerGoalFromCodex ( goal ) ) ;
7324+ if ( activation . stopped ) {
7325+ const paused = yield * client . request ( "thread/goal/set" , {
7326+ threadId,
7327+ status : "paused" ,
7328+ } ) ;
7329+ goalsByNativeThread . set ( threadId , providerGoalFromCodex ( paused . goal ) ) ;
7330+ }
7331+ } ) . pipe (
7332+ Effect . ensuring (
7333+ turnTerminalizationPermit . withPermits ( 1 ) (
7334+ Effect . gen ( function * ( ) {
7335+ if ( goalActivations . get ( threadId ) === activation ) {
7336+ goalActivations . delete ( threadId ) ;
7337+ }
7338+ if ( goalsByNativeThread . get ( threadId ) ?. status !== "active" ) {
7339+ yield * releaseGoalHold ( threadId ) ;
7340+ }
7341+ } ) ,
7342+ ) ,
7343+ ) ,
7344+ ) ;
7345+ } ) ;
7346+
72787347 /**
72797348 * Setting or resuming a goal starts the first goal turn with this run's
72807349 * turn configuration (approvals, sandbox, model, plan mode), since a
@@ -7303,6 +7372,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73037372 current === null ? "No goal is set." : describeCodexGoal ( current ) ,
73047373 ) ;
73057374 }
7375+ goalsPausedForWake . delete ( threadId ) ;
73067376 if ( command . type === "clear" ) {
73077377 const { cleared } = yield * client . request ( "thread/goal/clear" , { threadId } ) ;
73087378 goalsByNativeThread . set ( threadId , null ) ;
@@ -7330,50 +7400,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73307400 } ) ;
73317401 goalsByNativeThread . set ( threadId , providerGoalFromCodex ( goal ) ) ;
73327402 }
7333- // Hold fast first turns until activation; Stop cancels activation.
7334- const activation = { stopped : false } ;
7335- goalActivations . set ( threadId , activation ) ;
7336- yield * Effect . gen ( function * ( ) {
7337- // A bare objective gets echoed back, and "start this goal" gets a
7338- // create_goal call that Codex rejects because the goal already exists.
7339- yield * startNativeTurn (
7340- turnInput ,
7341- command . type === "set"
7342- ? yield * toCodexInput ( {
7343- ...turnInput ,
7344- message : {
7345- ...turnInput . message ,
7346- text : `This thread's goal is already set. Do not call create_goal. Start working on it now: ${ command . objective } ` ,
7347- } ,
7348- } )
7349- : [ ] ,
7350- ) ;
7351- if ( activation . stopped ) return ;
7352- const { goal } = yield * client . request ( "thread/goal/set" , {
7353- threadId,
7354- status : "active" ,
7355- } ) ;
7356- goalsByNativeThread . set ( threadId , providerGoalFromCodex ( goal ) ) ;
7357- if ( activation . stopped ) {
7358- const paused = yield * client . request ( "thread/goal/set" , {
7359- threadId,
7360- status : "paused" ,
7361- } ) ;
7362- goalsByNativeThread . set ( threadId , providerGoalFromCodex ( paused . goal ) ) ;
7363- }
7364- } ) . pipe (
7365- Effect . ensuring (
7366- turnTerminalizationPermit . withPermits ( 1 ) (
7367- Effect . gen ( function * ( ) {
7368- if ( goalActivations . get ( threadId ) === activation ) {
7369- goalActivations . delete ( threadId ) ;
7370- }
7371- if ( goalsByNativeThread . get ( threadId ) ?. status !== "active" ) {
7372- yield * releaseGoalHold ( threadId ) ;
7373- }
7374- } ) ,
7375- ) ,
7376- ) ,
7403+ // A bare objective gets echoed back, and "start this goal" gets a
7404+ // create_goal call that Codex rejects because the goal already exists.
7405+ yield * activateGoal (
7406+ turnInput ,
7407+ threadId ,
7408+ command . type === "set"
7409+ ? toCodexInput ( {
7410+ ...turnInput ,
7411+ message : {
7412+ ...turnInput . message ,
7413+ text : `This thread's goal is already set. Do not call create_goal. Start working on it now: ${ command . objective } ` ,
7414+ } ,
7415+ } )
7416+ : Effect . succeed ( [ ] ) ,
73777417 ) ;
73787418 } ,
73797419 ( effect , turnInput ) =>
@@ -7669,7 +7709,16 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
76697709 ? parseGoalCommand ( turnInput . message )
76707710 : null ;
76717711 if ( goalCommand !== null ) return runGoalCommand ( turnInput , goalCommand ) ;
7712+ const nativeThreadId = turnInput . providerThread . nativeThreadRef ?. nativeId ;
76727713 return Effect . gen ( function * ( ) {
7714+ if (
7715+ turnInput . restartContinuationOfRunId === undefined &&
7716+ nativeThreadId != null &&
7717+ goalsPausedForWake . has ( nativeThreadId ) &&
7718+ goalsByNativeThread . get ( nativeThreadId ) ?. status === "paused"
7719+ ) {
7720+ return yield * activateGoal ( turnInput , nativeThreadId , toCodexInput ( turnInput ) ) ;
7721+ }
76737722 yield * startNativeTurn (
76747723 turnInput ,
76757724 turnInput . restartContinuationOfRunId === undefined
0 commit comments