@@ -1681,34 +1681,46 @@ impl Agent {
16811681 instructions : String ,
16821682 ) {
16831683 let auth_data = parse_auth_result ( tool_result) ;
1684- {
1684+ let thread_exists = {
16851685 let mut sess = session. lock ( ) . await ;
1686- if let Some ( thread) = sess. threads . get_mut ( & thread_id) {
1687- thread. enter_auth_mode ( ext_name. clone ( ) ) ;
1688- thread. complete_turn ( & instructions) ;
1689- // User message already persisted at turn start; save auth instructions
1690- self . persist_assistant_response (
1691- thread_id,
1686+ match sess. threads . get_mut ( & thread_id) {
1687+ Some ( thread) => {
1688+ thread. enter_auth_mode ( ext_name. clone ( ) ) ;
1689+ thread. complete_turn ( & instructions) ;
1690+ // User message already persisted at turn start; save auth instructions
1691+ self . persist_assistant_response (
1692+ thread_id,
1693+ & message. channel ,
1694+ & message. user_id ,
1695+ & instructions,
1696+ )
1697+ . await ;
1698+ true
1699+ }
1700+ None => {
1701+ tracing:: error!(
1702+ %thread_id,
1703+ "Thread disappeared during auth intercept — skipping AuthRequired event"
1704+ ) ;
1705+ false
1706+ }
1707+ }
1708+ } ;
1709+ if thread_exists {
1710+ let _ = self
1711+ . channels
1712+ . send_status (
16921713 & message. channel ,
1693- & message. user_id ,
1694- & instructions,
1714+ StatusUpdate :: AuthRequired {
1715+ extension_name : ext_name,
1716+ instructions : Some ( instructions. clone ( ) ) ,
1717+ auth_url : auth_data. auth_url ,
1718+ setup_url : auth_data. setup_url ,
1719+ } ,
1720+ & message. metadata ,
16951721 )
16961722 . await ;
1697- }
16981723 }
1699- let _ = self
1700- . channels
1701- . send_status (
1702- & message. channel ,
1703- StatusUpdate :: AuthRequired {
1704- extension_name : ext_name,
1705- instructions : Some ( instructions. clone ( ) ) ,
1706- auth_url : auth_data. auth_url ,
1707- setup_url : auth_data. setup_url ,
1708- } ,
1709- & message. metadata ,
1710- )
1711- . await ;
17121724 }
17131725
17141726 /// Handle an auth token submitted while the thread is in auth mode.
@@ -1728,8 +1740,16 @@ impl Agent {
17281740 // Clear auth mode regardless of outcome
17291741 {
17301742 let mut sess = session. lock ( ) . await ;
1731- if let Some ( thread) = sess. threads . get_mut ( & thread_id) {
1732- thread. pending_auth = None ;
1743+ match sess. threads . get_mut ( & thread_id) {
1744+ Some ( thread) => {
1745+ thread. pending_auth = None ;
1746+ }
1747+ None => {
1748+ tracing:: error!(
1749+ %thread_id,
1750+ "Thread disappeared while clearing auth mode"
1751+ ) ;
1752+ }
17331753 }
17341754 }
17351755
@@ -1766,8 +1786,16 @@ impl Agent {
17661786 Ok ( result) => {
17671787 {
17681788 let mut sess = session. lock ( ) . await ;
1769- if let Some ( thread) = sess. threads . get_mut ( & thread_id) {
1770- thread. enter_auth_mode ( pending. extension_name . clone ( ) ) ;
1789+ match sess. threads . get_mut ( & thread_id) {
1790+ Some ( thread) => {
1791+ thread. enter_auth_mode ( pending. extension_name . clone ( ) ) ;
1792+ }
1793+ None => {
1794+ tracing:: error!(
1795+ %thread_id,
1796+ "Thread disappeared while re-entering auth mode"
1797+ ) ;
1798+ }
17711799 }
17721800 }
17731801 let _ = self
@@ -1791,8 +1819,16 @@ impl Agent {
17911819 if matches ! ( e, crate :: extensions:: ExtensionError :: ValidationFailed ( _) ) {
17921820 {
17931821 let mut sess = session. lock ( ) . await ;
1794- if let Some ( thread) = sess. threads . get_mut ( & thread_id) {
1795- thread. enter_auth_mode ( pending. extension_name . clone ( ) ) ;
1822+ match sess. threads . get_mut ( & thread_id) {
1823+ Some ( thread) => {
1824+ thread. enter_auth_mode ( pending. extension_name . clone ( ) ) ;
1825+ }
1826+ None => {
1827+ tracing:: error!(
1828+ %thread_id,
1829+ "Thread disappeared while re-entering auth mode after validation failure"
1830+ ) ;
1831+ }
17961832 }
17971833 }
17981834 let _ = self
0 commit comments