@@ -391,7 +391,7 @@ impl Channel {
391391
392392 if messages. len ( ) == 1 {
393393 // Single message - process normally
394- let message = messages. into_iter ( ) . next ( ) . unwrap ( ) ;
394+ let message = messages. into_iter ( ) . next ( ) . ok_or_else ( || anyhow :: anyhow! ( "empty iterator after length check" ) ) ? ;
395395 self . handle_message ( message) . await
396396 } else {
397397 // Multiple messages - batch them
@@ -464,8 +464,7 @@ impl Channel {
464464 } ) ;
465465 self . conversation_context = Some (
466466 prompt_engine
467- . render_conversation_context ( & first. source , server_name, channel_name)
468- . expect ( "failed to render conversation context" ) ,
467+ . render_conversation_context ( & first. source , server_name, channel_name) ?,
469468 ) ;
470469 }
471470
@@ -557,7 +556,7 @@ impl Channel {
557556 // Build system prompt with coalesce hint
558557 let system_prompt = self
559558 . build_system_prompt_with_coalesce ( message_count, elapsed_secs, unique_sender_count)
560- . await ;
559+ . await ? ;
561560
562561 {
563562 let mut reply_target = self . state . reply_target_message_id . write ( ) . await ;
@@ -594,21 +593,20 @@ impl Channel {
594593 message_count : usize ,
595594 elapsed_secs : f64 ,
596595 unique_senders : usize ,
597- ) -> String {
596+ ) -> Result < String > {
598597 let rc = & self . deps . runtime_config ;
599598 let prompt_engine = rc. prompts . load ( ) ;
600599
601600 let identity_context = rc. identity . load ( ) . render ( ) ;
602601 let memory_bulletin = rc. memory_bulletin . load ( ) ;
603602 let skills = rc. skills . load ( ) ;
604- let skills_prompt = skills. render_channel_prompt ( & prompt_engine) ;
603+ let skills_prompt = skills. render_channel_prompt ( & prompt_engine) ? ;
605604
606605 let browser_enabled = rc. browser_config . load ( ) . enabled ;
607606 let web_search_enabled = rc. brave_search_key . load ( ) . is_some ( ) ;
608607 let opencode_enabled = rc. opencode . load ( ) . enabled ;
609- let worker_capabilities = prompt_engine
610- . render_worker_capabilities ( browser_enabled, web_search_enabled, opencode_enabled)
611- . expect ( "failed to render worker capabilities" ) ;
608+ let worker_capabilities =
609+ prompt_engine. render_worker_capabilities ( browser_enabled, web_search_enabled, opencode_enabled) ?;
612610
613611 let status_text = {
614612 let status = self . state . status_block . read ( ) . await ;
@@ -625,18 +623,16 @@ impl Channel {
625623
626624 let empty_to_none = |s : String | if s. is_empty ( ) { None } else { Some ( s) } ;
627625
628- prompt_engine
629- . render_channel_prompt (
630- empty_to_none ( identity_context) ,
631- empty_to_none ( memory_bulletin. to_string ( ) ) ,
632- empty_to_none ( skills_prompt) ,
633- worker_capabilities,
634- self . conversation_context . clone ( ) ,
635- empty_to_none ( status_text) ,
636- coalesce_hint,
637- available_channels,
638- )
639- . expect ( "failed to render channel prompt" )
626+ prompt_engine. render_channel_prompt (
627+ empty_to_none ( identity_context) ,
628+ empty_to_none ( memory_bulletin. to_string ( ) ) ,
629+ empty_to_none ( skills_prompt) ,
630+ worker_capabilities,
631+ self . conversation_context . clone ( ) ,
632+ empty_to_none ( status_text) ,
633+ coalesce_hint,
634+ available_channels,
635+ )
640636 }
641637
642638 /// Handle an incoming message by running the channel's LLM agent loop.
@@ -718,12 +714,11 @@ impl Channel {
718714 } ) ;
719715 self . conversation_context = Some (
720716 prompt_engine
721- . render_conversation_context ( & message. source , server_name, channel_name)
722- . expect ( "failed to render conversation context" ) ,
717+ . render_conversation_context ( & message. source , server_name, channel_name) ?,
723718 ) ;
724719 }
725720
726- let system_prompt = self . build_system_prompt ( ) . await ;
721+ let system_prompt = self . build_system_prompt ( ) . await ? ;
727722
728723 {
729724 let mut reply_target = self . state . reply_target_message_id . write ( ) . await ;
@@ -795,21 +790,20 @@ impl Channel {
795790 }
796791
797792 /// Assemble the full system prompt using the PromptEngine.
798- async fn build_system_prompt ( & self ) -> String {
793+ async fn build_system_prompt ( & self ) -> crate :: error :: Result < String > {
799794 let rc = & self . deps . runtime_config ;
800795 let prompt_engine = rc. prompts . load ( ) ;
801796
802797 let identity_context = rc. identity . load ( ) . render ( ) ;
803798 let memory_bulletin = rc. memory_bulletin . load ( ) ;
804799 let skills = rc. skills . load ( ) ;
805- let skills_prompt = skills. render_channel_prompt ( & prompt_engine) ;
800+ let skills_prompt = skills. render_channel_prompt ( & prompt_engine) ? ;
806801
807802 let browser_enabled = rc. browser_config . load ( ) . enabled ;
808803 let web_search_enabled = rc. brave_search_key . load ( ) . is_some ( ) ;
809804 let opencode_enabled = rc. opencode . load ( ) . enabled ;
810805 let worker_capabilities = prompt_engine
811- . render_worker_capabilities ( browser_enabled, web_search_enabled, opencode_enabled)
812- . expect ( "failed to render worker capabilities" ) ;
806+ . render_worker_capabilities ( browser_enabled, web_search_enabled, opencode_enabled) ?;
813807
814808 let status_text = {
815809 let status = self . state . status_block . read ( ) . await ;
@@ -831,7 +825,6 @@ impl Channel {
831825 None , // coalesce_hint - only set for batched messages
832826 available_channels,
833827 )
834- . expect ( "failed to render channel prompt" )
835828 }
836829
837830 /// Register per-turn tools, run the LLM agentic loop, and clean up.
@@ -1177,13 +1170,19 @@ impl Channel {
11771170 "firing debounced retrigger"
11781171 ) ;
11791172
1180- let retrigger_message = self
1173+ let retrigger_message = match self
11811174 . deps
11821175 . runtime_config
11831176 . prompts
11841177 . load ( )
11851178 . render_system_retrigger ( )
1186- . expect ( "failed to render retrigger message" ) ;
1179+ {
1180+ Ok ( message) => message,
1181+ Err ( error) => {
1182+ tracing:: error!( %error, "failed to render retrigger message" ) ;
1183+ return ;
1184+ }
1185+ } ;
11871186
11881187 let synthetic = InboundMessage {
11891188 id : uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
@@ -1255,7 +1254,7 @@ pub async fn spawn_branch_from_state(
12551254 & rc. instance_dir . display ( ) . to_string ( ) ,
12561255 & rc. workspace_dir . display ( ) . to_string ( ) ,
12571256 )
1258- . expect ( "failed to render branch prompt" ) ;
1257+ . map_err ( |e| AgentError :: Other ( anyhow :: anyhow! ( "{e}" ) ) ) ? ;
12591258
12601259 spawn_branch (
12611260 state,
@@ -1279,10 +1278,10 @@ async fn spawn_memory_persistence_branch(
12791278 let prompt_engine = deps. runtime_config . prompts . load ( ) ;
12801279 let system_prompt = prompt_engine
12811280 . render_static ( "memory_persistence" )
1282- . expect ( "failed to render memory_persistence prompt" ) ;
1281+ . map_err ( |e| AgentError :: Other ( anyhow :: anyhow! ( "{e}" ) ) ) ? ;
12831282 let prompt = prompt_engine
12841283 . render_system_memory_persistence ( )
1285- . expect ( "failed to render memory persistence prompt" ) ;
1284+ . map_err ( |e| AgentError :: Other ( anyhow :: anyhow! ( "{e}" ) ) ) ? ;
12861285
12871286 spawn_branch (
12881287 state,
@@ -1413,7 +1412,7 @@ pub async fn spawn_worker_from_state(
14131412 & rc. instance_dir . display ( ) . to_string ( ) ,
14141413 & rc. workspace_dir . display ( ) . to_string ( ) ,
14151414 )
1416- . expect ( "failed to render worker prompt" ) ;
1415+ . map_err ( |e| AgentError :: Other ( anyhow :: anyhow! ( "{e}" ) ) ) ? ;
14171416 let skills = rc. skills . load ( ) ;
14181417 let browser_config = ( * * rc. browser_config . load ( ) ) . clone ( ) ;
14191418 let brave_search_key = ( * * rc. brave_search_key . load ( ) ) . clone ( ) ;
0 commit comments