@@ -4388,6 +4388,284 @@ fn try_native_steer(
43884388 }
43894389}
43904390
4391+ // ── try_native_steer fallback-log tests ───────────────────────────────────────
4392+ //
4393+ // Regression for the production tracing event `try_native_steer`'s Err arm
4394+ // emits before the caller falls back to the universal cancel+merge path. The
4395+ // `send_steer` admission-reason tests in pool.rs pin the refusal *labels*
4396+ // through `SteerError::admission_reason` alone — mutating this log's `reason`
4397+ // field leaves that suite green. These tests instead drive the REAL
4398+ // `try_native_steer` (real pool, real queue, real signed event, real steer
4399+ // body construction) through each of the four admission refusal branches and
4400+ // pin the log itself: the exact reason label, the exact production message,
4401+ // exactly the `channel`+`reason` fields (never request or error content), and
4402+ // the unchanged `false` return that keeps the caller on the fallback.
4403+ #[ cfg( test) ]
4404+ mod try_native_steer_fallback_log_tests {
4405+ use super :: * ;
4406+ use crate :: pool:: { SteerRequest , TaskMeta } ;
4407+ use nostr:: { EventBuilder , Keys , Kind } ;
4408+ use tracing_subscriber:: layer:: SubscriberExt ;
4409+
4410+ /// Sentinel carried in the real event content — it flows into the steer
4411+ /// request body `try_native_steer` builds, and the fallback log must
4412+ /// never surface it.
4413+ const SECRET_REQUEST_CONTENT : & str = "SECRET-STEER-REQUEST-CONTENT" ;
4414+
4415+ /// One captured fallback log event: the exact values recorded for the
4416+ /// message/channel/reason fields, plus the names of any field beyond
4417+ /// that fixed vocabulary.
4418+ #[ derive( Debug , Default ) ]
4419+ struct FallbackLog {
4420+ message : Option < String > ,
4421+ channel : Option < String > ,
4422+ reason : Option < String > ,
4423+ unexpected_fields : Vec < String > ,
4424+ }
4425+
4426+ /// Records event field values (str values via `record_str`, Display and
4427+ /// format_args values via `record_debug`, exactly as tracing routes them)
4428+ /// and collects any field outside the fixed message/channel/reason set.
4429+ #[ derive( Debug , Default ) ]
4430+ struct Recorder {
4431+ log : FallbackLog ,
4432+ }
4433+
4434+ impl Recorder {
4435+ fn store ( & mut self , name : & str , value : String ) {
4436+ match name {
4437+ "message" => self . log . message = Some ( value) ,
4438+ "channel" => self . log . channel = Some ( value) ,
4439+ "reason" => self . log . reason = Some ( value) ,
4440+ _ => self . log . unexpected_fields . push ( name. to_string ( ) ) ,
4441+ }
4442+ }
4443+ }
4444+
4445+ impl tracing:: field:: Visit for Recorder {
4446+ fn record_str ( & mut self , field : & tracing:: field:: Field , value : & str ) {
4447+ self . store ( field. name ( ) , value. to_string ( ) ) ;
4448+ }
4449+
4450+ fn record_debug ( & mut self , field : & tracing:: field:: Field , value : & dyn std:: fmt:: Debug ) {
4451+ self . store ( field. name ( ) , format ! ( "{value:?}" ) ) ;
4452+ }
4453+ }
4454+
4455+ /// Captures INFO events on `buzz_acp` carrying a `reason` field — the
4456+ /// fallback log's signature — mirroring the workspace's Layer+Visit
4457+ /// capture fixture (buzz-agent `count_silent_turn_warnings`).
4458+ struct Capture {
4459+ logs : Arc < std:: sync:: Mutex < Vec < FallbackLog > > > ,
4460+ }
4461+
4462+ impl < S : tracing:: Subscriber > tracing_subscriber:: Layer < S > for Capture {
4463+ fn on_event (
4464+ & self ,
4465+ event : & tracing:: Event < ' _ > ,
4466+ _: tracing_subscriber:: layer:: Context < ' _ , S > ,
4467+ ) {
4468+ if * event. metadata ( ) . level ( ) != tracing:: Level :: INFO {
4469+ return ;
4470+ }
4471+ if event. metadata ( ) . target ( ) != "buzz_acp" {
4472+ return ;
4473+ }
4474+ let mut recorder = Recorder :: default ( ) ;
4475+ event. record ( & mut recorder) ;
4476+ if recorder. log . reason . is_some ( ) {
4477+ self . logs
4478+ . lock ( )
4479+ . unwrap ( )
4480+ . push ( std:: mem:: take ( & mut recorder. log ) ) ;
4481+ }
4482+ }
4483+ }
4484+
4485+ /// Conversation scope for the steered channel — the same shape the
4486+ /// pool.rs admission fixtures use.
4487+ fn steer_scope ( ) -> scope:: SessionScope {
4488+ scope:: SessionScope :: Conversation {
4489+ channel_id : Uuid :: nil ( ) ,
4490+ }
4491+ }
4492+
4493+ /// A real signed kind:20001 stream message whose content becomes part of
4494+ /// the real steer request body `try_native_steer` builds.
4495+ fn stream_event ( ) -> nostr:: Event {
4496+ EventBuilder :: new (
4497+ Kind :: Custom ( KIND_STREAM_MESSAGE as u16 ) ,
4498+ SECRET_REQUEST_CONTENT ,
4499+ )
4500+ . sign_with_keys ( & Keys :: generate ( ) )
4501+ . expect ( "sign test stream message" )
4502+ }
4503+
4504+ /// Insert an in-flight task_map entry for `scope` carrying `steer_tx`,
4505+ /// mirroring `mark_agent_busy` — the existing seam for simulating an
4506+ /// in-flight prompt task without spawning a real agent turn (same shape
4507+ /// as the pool.rs `mark_agent_busy_with_steer_tx` fixture).
4508+ fn mark_in_flight_with_steer_tx (
4509+ pool : & mut AgentPool ,
4510+ busy_scope : scope:: SessionScope ,
4511+ steer_tx : Option < tokio:: sync:: mpsc:: Sender < SteerRequest > > ,
4512+ ) {
4513+ let abort = pool. join_set . spawn ( async { } ) ;
4514+ pool. task_map_mut ( ) . insert (
4515+ abort. id ( ) ,
4516+ TaskMeta {
4517+ agent_index : 0 ,
4518+ channel_id : Some ( busy_scope. channel_id ( ) ) ,
4519+ scope : Some ( busy_scope) ,
4520+ turn_id : "t" . into ( ) ,
4521+ recoverable_batch : None ,
4522+ control_tx : None ,
4523+ steer_tx,
4524+ successful_steer_deliveries : HashSet :: new ( ) ,
4525+ } ,
4526+ ) ;
4527+ }
4528+
4529+ /// Drive the REAL `try_native_steer` exactly as the production caller
4530+ /// (`QueuedNormalListenerEvent::steer_or_interrupt`) does — event already
4531+ /// pushed into the queue, steer-eligible event, live steer-ack channel —
4532+ /// under a capturing subscriber. Returns the function's return value and
4533+ /// the fallback logs it emitted on this thread.
4534+ fn try_native_steer_capturing ( pool : & mut AgentPool ) -> ( bool , Vec < FallbackLog > ) {
4535+ let busy_scope = steer_scope ( ) ;
4536+ let event = stream_event ( ) ;
4537+ let mut queue = EventQueue :: new ( DedupMode :: Queue ) ;
4538+ // Caller invariant: the event is already queued before the steer
4539+ // attempt (see `try_native_steer`'s doc comment).
4540+ assert ! (
4541+ queue. push( QueuedEvent {
4542+ channel_id: Uuid :: nil( ) ,
4543+ scope: busy_scope. clone( ) ,
4544+ event: event. clone( ) ,
4545+ received_at: std:: time:: Instant :: now( ) ,
4546+ prompt_tag: "mention" . into( ) ,
4547+ } ) ,
4548+ "queued event must be accepted before the steer attempt"
4549+ ) ;
4550+ let ( steer_ack_tx, _steer_ack_rx) = mpsc:: unbounded_channel :: < SteerAckEvent > ( ) ;
4551+ let logs = Arc :: new ( std:: sync:: Mutex :: new ( Vec :: new ( ) ) ) ;
4552+ let subscriber = tracing_subscriber:: registry ( ) . with ( Capture { logs : logs. clone ( ) } ) ;
4553+ let returned = tracing:: subscriber:: with_default ( subscriber, || {
4554+ try_native_steer (
4555+ pool,
4556+ & mut queue,
4557+ busy_scope. clone ( ) ,
4558+ event,
4559+ "mention" . into ( ) ,
4560+ & steer_ack_tx,
4561+ )
4562+ } ) ;
4563+ let logs_out = std:: mem:: take ( & mut * logs. lock ( ) . unwrap ( ) ) ;
4564+ ( returned, logs_out)
4565+ }
4566+
4567+ /// Assert the captured logs are exactly one production fallback event for
4568+ /// `expected_reason`: exact message, exact reason, exact channel, no
4569+ /// additional fields, and no request or error content anywhere.
4570+ fn assert_single_fallback_log ( logs : & [ FallbackLog ] , expected_reason : & str ) {
4571+ assert_eq ! (
4572+ logs. len( ) ,
4573+ 1 ,
4574+ "exactly one fallback log must be emitted per refusal, got {logs:?}"
4575+ ) ;
4576+ let log = & logs[ 0 ] ;
4577+ assert_eq ! (
4578+ log. message. as_deref( ) ,
4579+ Some ( "non-cancelling steer not accepted — falling back to cancel+merge" ) ,
4580+ "fallback log message must stay the exact production bytes"
4581+ ) ;
4582+ assert_eq ! (
4583+ log. reason. as_deref( ) ,
4584+ Some ( expected_reason) ,
4585+ "fallback log reason must be the exact admission label"
4586+ ) ;
4587+ let expected_channel = Uuid :: nil ( ) . to_string ( ) ;
4588+ assert_eq ! (
4589+ log. channel. as_deref( ) ,
4590+ Some ( expected_channel. as_str( ) ) ,
4591+ "fallback log channel must be the scope's channel id"
4592+ ) ;
4593+ assert ! (
4594+ log. unexpected_fields. is_empty( ) ,
4595+ "fallback log must carry only channel+reason — no request/error \
4596+ content fields: {:?}",
4597+ log. unexpected_fields
4598+ ) ;
4599+ let rendered = format ! ( "{log:?}" ) ;
4600+ assert ! (
4601+ !rendered. contains( SECRET_REQUEST_CONTENT ) ,
4602+ "fallback log must not leak request content: {rendered}"
4603+ ) ;
4604+ }
4605+
4606+ /// No in-flight task owns the scope: `send_steer` refuses with
4607+ /// `PromptCompleted`, classified as `task_absent`.
4608+ #[ test]
4609+ fn try_native_steer_logs_task_absent_reason_and_keeps_fallback ( ) {
4610+ let mut pool = AgentPool :: from_slots ( vec ! [ ] ) ;
4611+ let ( returned, logs) = try_native_steer_capturing ( & mut pool) ;
4612+ assert ! (
4613+ !returned,
4614+ "refused native steer must keep the cancel+merge fallback"
4615+ ) ;
4616+ assert_single_fallback_log ( & logs, "task_absent" ) ;
4617+ }
4618+
4619+ /// The in-flight task has no steer sender installed: `sender_absent`.
4620+ #[ tokio:: test]
4621+ async fn try_native_steer_logs_sender_absent_reason_and_keeps_fallback ( ) {
4622+ let mut pool = AgentPool :: from_slots ( vec ! [ ] ) ;
4623+ mark_in_flight_with_steer_tx ( & mut pool, steer_scope ( ) , None ) ;
4624+ let ( returned, logs) = try_native_steer_capturing ( & mut pool) ;
4625+ assert ! (
4626+ !returned,
4627+ "refused native steer must keep the cancel+merge fallback"
4628+ ) ;
4629+ assert_single_fallback_log ( & logs, "sender_absent" ) ;
4630+ }
4631+
4632+ /// The capacity-1 steer mailbox already holds one in-flight steer:
4633+ /// `mailbox_full`.
4634+ #[ tokio:: test]
4635+ async fn try_native_steer_logs_mailbox_full_reason_and_keeps_fallback ( ) {
4636+ let mut pool = AgentPool :: from_slots ( vec ! [ ] ) ;
4637+ let ( tx, _rx) = tokio:: sync:: mpsc:: channel :: < SteerRequest > ( 1 ) ;
4638+ mark_in_flight_with_steer_tx ( & mut pool, steer_scope ( ) , Some ( tx. clone ( ) ) ) ;
4639+ let ( ack_tx, _ack_rx) = tokio:: sync:: oneshot:: channel :: < pool:: SteerAck > ( ) ;
4640+ tx. try_send ( SteerRequest {
4641+ prompt_blocks : vec ! [ "first in-flight steer" . into( ) ] ,
4642+ ack_tx,
4643+ } )
4644+ . expect ( "capacity-1 mailbox accepts the first in-flight steer" ) ;
4645+ let ( returned, logs) = try_native_steer_capturing ( & mut pool) ;
4646+ assert ! (
4647+ !returned,
4648+ "refused native steer must keep the cancel+merge fallback"
4649+ ) ;
4650+ assert_single_fallback_log ( & logs, "mailbox_full" ) ;
4651+ }
4652+
4653+ /// The read loop's steer receiver is torn down: `mailbox_closed`.
4654+ #[ tokio:: test]
4655+ async fn try_native_steer_logs_mailbox_closed_reason_and_keeps_fallback ( ) {
4656+ let mut pool = AgentPool :: from_slots ( vec ! [ ] ) ;
4657+ let ( tx, rx) = tokio:: sync:: mpsc:: channel :: < SteerRequest > ( 1 ) ;
4658+ mark_in_flight_with_steer_tx ( & mut pool, steer_scope ( ) , Some ( tx) ) ;
4659+ drop ( rx) ; // read loop torn down before this steer arrived
4660+ let ( returned, logs) = try_native_steer_capturing ( & mut pool) ;
4661+ assert ! (
4662+ !returned,
4663+ "refused native steer must keep the cancel+merge fallback"
4664+ ) ;
4665+ assert_single_fallback_log ( & logs, "mailbox_closed" ) ;
4666+ }
4667+ }
4668+
43914669// ── dispatch_pending ──────────────────────────────────────────────────────────
43924670
43934671/// Flush queued work to available agents.
0 commit comments