@@ -15,16 +15,20 @@ use codex_core::protocol::McpToolCallEndEvent;
1515use codex_core:: protocol:: PatchApplyBeginEvent ;
1616use codex_core:: protocol:: PatchApplyEndEvent ;
1717use codex_core:: protocol:: SessionConfiguredEvent ;
18+ use codex_core:: protocol:: TaskCompleteEvent ;
1819use codex_core:: protocol:: TokenUsage ;
1920use owo_colors:: OwoColorize ;
2021use owo_colors:: Style ;
2122use shlex:: try_join;
2223use std:: collections:: HashMap ;
2324use std:: io:: Write ;
25+ use std:: path:: PathBuf ;
2426use std:: time:: Instant ;
2527
28+ use crate :: event_processor:: CodexStatus ;
2629use crate :: event_processor:: EventProcessor ;
2730use crate :: event_processor:: create_config_summary_entries;
31+ use crate :: event_processor:: handle_last_message;
2832
2933/// This should be configurable. When used in CI, users may not want to impose
3034/// a limit so they can see the full transcript.
@@ -54,10 +58,15 @@ pub(crate) struct EventProcessorWithHumanOutput {
5458 show_agent_reasoning : bool ,
5559 answer_started : bool ,
5660 reasoning_started : bool ,
61+ last_message_path : Option < PathBuf > ,
5762}
5863
5964impl EventProcessorWithHumanOutput {
60- pub ( crate ) fn create_with_ansi ( with_ansi : bool , config : & Config ) -> Self {
65+ pub ( crate ) fn create_with_ansi (
66+ with_ansi : bool ,
67+ config : & Config ,
68+ last_message_path : Option < PathBuf > ,
69+ ) -> Self {
6170 let call_id_to_command = HashMap :: new ( ) ;
6271 let call_id_to_patch = HashMap :: new ( ) ;
6372 let call_id_to_tool_call = HashMap :: new ( ) ;
@@ -77,6 +86,7 @@ impl EventProcessorWithHumanOutput {
7786 show_agent_reasoning : !config. hide_agent_reasoning ,
7887 answer_started : false ,
7988 reasoning_started : false ,
89+ last_message_path,
8090 }
8191 } else {
8292 Self {
@@ -93,6 +103,7 @@ impl EventProcessorWithHumanOutput {
93103 show_agent_reasoning : !config. hide_agent_reasoning ,
94104 answer_started : false ,
95105 reasoning_started : false ,
106+ last_message_path,
96107 }
97108 }
98109 }
@@ -158,7 +169,7 @@ impl EventProcessor for EventProcessorWithHumanOutput {
158169 ) ;
159170 }
160171
161- fn process_event ( & mut self , event : Event ) {
172+ fn process_event ( & mut self , event : Event ) -> CodexStatus {
162173 let Event { id : _, msg } = event;
163174 match msg {
164175 EventMsg :: Error ( ErrorEvent { message } ) => {
@@ -168,9 +179,16 @@ impl EventProcessor for EventProcessorWithHumanOutput {
168179 EventMsg :: BackgroundEvent ( BackgroundEventEvent { message } ) => {
169180 ts_println ! ( self , "{}" , message. style( self . dimmed) ) ;
170181 }
171- EventMsg :: TaskStarted | EventMsg :: TaskComplete ( _ ) => {
182+ EventMsg :: TaskStarted => {
172183 // Ignore.
173184 }
185+ EventMsg :: TaskComplete ( TaskCompleteEvent { last_agent_message } ) => {
186+ handle_last_message (
187+ last_agent_message. as_deref ( ) ,
188+ self . last_message_path . as_deref ( ) ,
189+ ) ;
190+ return CodexStatus :: InitiateShutdown ;
191+ }
174192 EventMsg :: TokenCount ( TokenUsage { total_tokens, .. } ) => {
175193 ts_println ! ( self , "tokens used: {total_tokens}" ) ;
176194 }
@@ -185,7 +203,7 @@ impl EventProcessor for EventProcessorWithHumanOutput {
185203 }
186204 EventMsg :: AgentReasoningDelta ( AgentReasoningDeltaEvent { delta } ) => {
187205 if !self . show_agent_reasoning {
188- return ;
206+ return CodexStatus :: Running ;
189207 }
190208 if !self . reasoning_started {
191209 ts_println ! (
@@ -498,7 +516,9 @@ impl EventProcessor for EventProcessorWithHumanOutput {
498516 EventMsg :: GetHistoryEntryResponse ( _) => {
499517 // Currently ignored in exec output.
500518 }
519+ EventMsg :: ShutdownComplete => return CodexStatus :: Shutdown ,
501520 }
521+ CodexStatus :: Running
502522 }
503523}
504524
0 commit comments