@@ -1776,7 +1776,6 @@ async fn tokio_main() -> Result<()> {
17761776 & respawn_tx,
17771777 & mut respawn_tasks,
17781778 observer. clone ( ) ,
1779- & relay,
17801779 ) == LoopAction :: Exit
17811780 {
17821781 break ;
@@ -2071,23 +2070,13 @@ fn handle_prompt_result(
20712070 respawn_tx : & mpsc:: Sender < RespawnResult > ,
20722071 respawn_tasks : & mut tokio:: task:: JoinSet < ( ) > ,
20732072 observer : Option < observer:: ObserverHandle > ,
2074- relay : & HarnessRelay ,
20752073) -> LoopAction {
20762074 let before = pool. task_map ( ) . len ( ) ;
20772075 let agent_index = result. agent . index ;
20782076 pool. task_map_mut ( )
20792077 . retain ( |_, meta| meta. agent_index != agent_index) ;
20802078 debug_assert_eq ! ( before, pool. task_map( ) . len( ) + 1 ) ;
20812079
2082- // Extract thread root from the batch before it's consumed by requeue.
2083- // Used by death notices to thread the message into the original conversation.
2084- let thread_root: Option < String > = result
2085- . batch
2086- . as_ref ( )
2087- . and_then ( |b| b. events . first ( ) )
2088- . map ( |e| queue:: parse_thread_tags ( & e. event ) )
2089- . and_then ( |tags| tags. root_event_id ) ;
2090-
20912080 // Requeue BEFORE mark_complete: requeue() sets retry_after with a future
20922081 // deadline, and mark_complete() checks for it to decide whether to preserve
20932082 // retry_counts. If mark_complete runs first, retry_counts is cleared and
@@ -2176,11 +2165,6 @@ fn handle_prompt_result(
21762165 } ;
21772166 emit_turn_error ( death_message) ;
21782167
2179- // Post a visible death notice to the channel so humans know why
2180- // the agent went silent.
2181- if let Some ( ch) = channel_id {
2182- relay. publish_death_notice ( ch, death_message, thread_root. as_deref ( ) ) ;
2183- }
21842168 let index = result. agent . index ;
21852169 let slot_history = & mut crash_history[ index] ;
21862170 if !spawn_respawn_task (
@@ -2238,15 +2222,6 @@ fn handle_prompt_result(
22382222 ) ;
22392223 emit_turn_error ( & e. to_string ( ) ) ;
22402224
2241- // Post a visible death notice for transport errors too.
2242- if let Some ( ch) = channel_id {
2243- relay. publish_death_notice (
2244- ch,
2245- "Agent connection lost (transport error)" ,
2246- thread_root. as_deref ( ) ,
2247- ) ;
2248- }
2249-
22502225 let index = result. agent . index ;
22512226 let slot_history = & mut crash_history[ index] ;
22522227 if !spawn_respawn_task (
@@ -3146,3 +3121,168 @@ mod build_mcp_servers_tests {
31463121 ) ;
31473122 }
31483123}
3124+
3125+ #[ cfg( test) ]
3126+ mod error_outcome_emission_tests {
3127+ //! Pins the policy that error-class outcomes surface to the activity feed
3128+ //! and never to the channel:
3129+ //!
3130+ //! - Channel silence is enforced *structurally* — `handle_prompt_result`
3131+ //! takes no relay handle, so it has no way to post a channel message. A
3132+ //! future re-introduction of channel notices would have to add the relay
3133+ //! parameter back, which these tests' construction would then refuse to
3134+ //! compile against.
3135+ //! - Feed coverage is the regression-prone half and is asserted at runtime:
3136+ //! each error outcome must emit exactly one `turn_error` observer event.
3137+ //! If any branch drops its `emit_turn_error` call, the matching test goes
3138+ //! red.
3139+
3140+ use super :: * ;
3141+ use crate :: acp:: { AcpClient , AcpError } ;
3142+ use crate :: observer:: ObserverHandle ;
3143+ use crate :: pool:: { AgentPool , OwnedAgent , PromptOutcome , PromptResult , PromptSource } ;
3144+ use std:: collections:: HashSet ;
3145+
3146+ fn test_config ( ) -> Config {
3147+ Config {
3148+ keys : nostr:: Keys :: generate ( ) ,
3149+ relay_url : "ws://localhost:3000" . into ( ) ,
3150+ // `true` exits cleanly, so the async respawn fails fast and
3151+ // harmlessly off the JoinSet — irrelevant to the synchronous
3152+ // feed emission under test.
3153+ agent_command : "true" . into ( ) ,
3154+ agent_args : vec ! [ ] ,
3155+ mcp_command : "test-mcp-server" . into ( ) ,
3156+ idle_timeout_secs : config:: DEFAULT_IDLE_TIMEOUT_SECS ,
3157+ max_turn_duration_secs : 3600 ,
3158+ agents : 1 ,
3159+ heartbeat_interval_secs : 0 ,
3160+ heartbeat_prompt : None ,
3161+ system_prompt : None ,
3162+ initial_message : None ,
3163+ subscribe_mode : config:: SubscribeMode :: All ,
3164+ dedup_mode : config:: DedupMode :: Queue ,
3165+ multiple_event_handling : config:: MultipleEventHandling :: Queue ,
3166+ ignore_self : true ,
3167+ kinds_override : None ,
3168+ channels_override : None ,
3169+ no_mention_filter : false ,
3170+ config_path : std:: path:: PathBuf :: from ( "./buzz-acp.toml" ) ,
3171+ context_message_limit : 12 ,
3172+ max_turns_per_session : 0 ,
3173+ presence_enabled : true ,
3174+ typing_enabled : true ,
3175+ memory_enabled : false ,
3176+ model : None ,
3177+ permission_mode : config:: PermissionMode :: BypassPermissions ,
3178+ respond_to : config:: RespondTo :: Anyone ,
3179+ respond_to_allowlist : HashSet :: new ( ) ,
3180+ persona_env_vars : vec ! [ ] ,
3181+ relay_observer : false ,
3182+ agent_owner : None ,
3183+ no_base_prompt : false ,
3184+ base_prompt_content : None ,
3185+ }
3186+ }
3187+
3188+ /// Spawn a real but inert agent subprocess (`cat`) so the error paths have
3189+ /// an `OwnedAgent` to move into respawn or return to the pool. The error
3190+ /// branches never talk to the subprocess.
3191+ async fn dummy_agent ( index : usize ) -> OwnedAgent {
3192+ OwnedAgent {
3193+ index,
3194+ acp : AcpClient :: spawn ( "cat" , & [ ] , & [ ] )
3195+ . await
3196+ . expect ( "spawn cat as inert agent" ) ,
3197+ state : Default :: default ( ) ,
3198+ model_capabilities : None ,
3199+ desired_model : None ,
3200+ // Error branches under test never read this; 1 is the legacy
3201+ // non-systemPrompt path, the simplest valid value.
3202+ protocol_version : 1 ,
3203+ }
3204+ }
3205+
3206+ /// Drive one error outcome through `handle_prompt_result` and return how
3207+ /// many `turn_error` events it emitted to the observer feed.
3208+ async fn turn_errors_emitted_for ( outcome : PromptOutcome ) -> usize {
3209+ let agent = dummy_agent ( 0 ) . await ;
3210+ let mut pool = AgentPool :: from_slots ( vec ! [ None ] ) ;
3211+
3212+ // `handle_prompt_result` asserts it removes exactly one in-flight task
3213+ // for the completing agent (the slot was checked out, not idle). Mirror
3214+ // the real dispatch path by registering a TaskMeta keyed on a genuine
3215+ // `task::Id` — only obtainable from inside a spawned task.
3216+ let task_id = pool. join_set . spawn ( async { } ) . id ( ) ;
3217+ pool. task_map_mut ( ) . insert (
3218+ task_id,
3219+ crate :: pool:: TaskMeta {
3220+ agent_index : 0 ,
3221+ channel_id : None ,
3222+ recoverable_batch : None ,
3223+ control_tx : None ,
3224+ } ,
3225+ ) ;
3226+
3227+ let mut queue = EventQueue :: new ( config:: DedupMode :: Queue ) ;
3228+ let config = test_config ( ) ;
3229+ let mut heartbeat_in_flight = false ;
3230+ let removed_channels = HashSet :: new ( ) ;
3231+ let mut crash_history = vec ! [ SlotCircuit {
3232+ crash_times: Vec :: new( ) ,
3233+ open_until: None ,
3234+ respawn_in_flight: false ,
3235+ } ] ;
3236+ let ( respawn_tx, _respawn_rx) = mpsc:: channel ( 8 ) ;
3237+ let mut respawn_tasks = tokio:: task:: JoinSet :: new ( ) ;
3238+ let observer = ObserverHandle :: in_process ( ) ;
3239+
3240+ let result = PromptResult {
3241+ agent,
3242+ source : PromptSource :: Channel ( Uuid :: new_v4 ( ) ) ,
3243+ outcome,
3244+ batch : None ,
3245+ } ;
3246+
3247+ handle_prompt_result (
3248+ & mut pool,
3249+ & mut queue,
3250+ & config,
3251+ result,
3252+ & mut heartbeat_in_flight,
3253+ & removed_channels,
3254+ & mut crash_history,
3255+ & respawn_tx,
3256+ & mut respawn_tasks,
3257+ Some ( observer. clone ( ) ) ,
3258+ ) ;
3259+
3260+ observer
3261+ . snapshot ( )
3262+ . iter ( )
3263+ . filter ( |e| e. kind == "turn_error" )
3264+ . count ( )
3265+ }
3266+
3267+ #[ tokio:: test]
3268+ async fn agent_exited_emits_exactly_one_feed_event ( ) {
3269+ assert_eq ! ( turn_errors_emitted_for( PromptOutcome :: AgentExited ) . await , 1 ) ;
3270+ }
3271+
3272+ #[ tokio:: test]
3273+ async fn timeout_emits_exactly_one_feed_event ( ) {
3274+ assert_eq ! ( turn_errors_emitted_for( PromptOutcome :: Timeout ) . await , 1 ) ;
3275+ }
3276+
3277+ #[ tokio:: test]
3278+ async fn transport_error_emits_exactly_one_feed_event ( ) {
3279+ let io = AcpError :: Io ( std:: io:: Error :: other ( "pipe broke" ) ) ;
3280+ assert_eq ! ( turn_errors_emitted_for( PromptOutcome :: Error ( io) ) . await , 1 ) ;
3281+ }
3282+
3283+ #[ tokio:: test]
3284+ async fn application_error_emits_exactly_one_feed_event ( ) {
3285+ let app = AcpError :: IdleTimeout ( std:: time:: Duration :: from_secs ( 1 ) ) ;
3286+ assert_eq ! ( turn_errors_emitted_for( PromptOutcome :: Error ( app) ) . await , 1 ) ;
3287+ }
3288+ }
0 commit comments