@@ -48,12 +48,17 @@ export interface CodexPlanResult {
4848 source : CodexPlanSource ;
4949}
5050
51+ export interface GetLastCodexMessageOptions {
52+ beforeActiveTurn ?: boolean ;
53+ }
54+
5155export interface GetLatestCodexPlanOptions {
5256 turnId ?: string ;
5357 stopHookActive ?: boolean ;
5458}
5559
5660const TURN_START_TYPES = new Set ( [ "task_started" , "turn_started" ] ) ;
61+ const TURN_COMPLETE_TYPES = new Set ( [ "task_complete" , "turn_completed" ] ) ;
5762const PROPOSED_PLAN_RE = / < p r o p o s e d _ p l a n > ( [ \s \S ] * ?) < \/ p r o p o s e d _ p l a n > / gi;
5863
5964// --- Rollout File Discovery ---
@@ -200,6 +205,24 @@ function findTurnStartIndex(entries: RolloutEntry[], turnId?: string): number {
200205 return lastTurnContext === - 1 ? 0 : lastTurnContext ;
201206}
202207
208+ function findActiveTurnStartIndex ( entries : RolloutEntry [ ] ) : number {
209+ const latestTurnStart = findLastIndex (
210+ entries ,
211+ ( entry ) =>
212+ entry . type === "event_msg" &&
213+ TURN_START_TYPES . has ( entry . payload ?. type || "" )
214+ ) ;
215+ if ( latestTurnStart === - 1 ) return - 1 ;
216+
217+ const latestTurnComplete = findLastIndex (
218+ entries ,
219+ ( entry ) =>
220+ entry . type === "event_msg" &&
221+ TURN_COMPLETE_TYPES . has ( entry . payload ?. type || "" )
222+ ) ;
223+ return latestTurnStart > latestTurnComplete ? latestTurnStart : - 1 ;
224+ }
225+
203226function isHookPromptMessage ( entry : RolloutEntry ) : boolean {
204227 if ( entry . type !== "response_item" ) return false ;
205228 if ( entry . payload ?. type !== "message" ) return false ;
@@ -295,12 +318,17 @@ function pickLatestPreferredPlan(
295318 * Extracts output_text blocks from payload.content.
296319 */
297320export function getLastCodexMessage (
298- rolloutPath : string
321+ rolloutPath : string ,
322+ options : GetLastCodexMessageOptions = { }
299323) : { text : string } | null {
300324 const entries = parseRolloutEntries ( rolloutPath ) ;
325+ const activeTurnStart = options . beforeActiveTurn
326+ ? findActiveTurnStartIndex ( entries )
327+ : - 1 ;
328+ const endIndex = activeTurnStart === - 1 ? entries . length - 1 : activeTurnStart - 1 ;
301329
302330 // Walk backward
303- for ( let i = entries . length - 1 ; i >= 0 ; i -- ) {
331+ for ( let i = endIndex ; i >= 0 ; i -- ) {
304332 const entry = entries [ i ] ;
305333 if ( entry . type !== "response_item" ) continue ;
306334 if ( entry . payload ?. type !== "message" ) continue ;
0 commit comments