@@ -253,6 +253,7 @@ function createManager(options?: {
253253 deferStartupAccountStartsUntil ?: Promise < void > ;
254254 fillChannelDependencies ?: boolean ;
255255 ambientAutostartSuppressedChannelIds ?: ReadonlySet < string > ;
256+ tryRecoverAutostartSuppression ?: ( ) => boolean ;
256257 getNativeApprovalRuntime ?: ( ) => GatewayNativeApprovalRuntime | undefined ;
257258} ) {
258259 const log = createSubsystemLogger ( "gateway/server-channels-test" ) ;
@@ -281,6 +282,9 @@ function createManager(options?: {
281282 ...( options ?. ambientAutostartSuppressedChannelIds
282283 ? { ambientAutostartSuppressedChannelIds : options . ambientAutostartSuppressedChannelIds }
283284 : { } ) ,
285+ ...( options ?. tryRecoverAutostartSuppression
286+ ? { tryRecoverAutostartSuppression : options . tryRecoverAutostartSuppression }
287+ : { } ) ,
284288 ...( options ?. getNativeApprovalRuntime
285289 ? { getNativeApprovalRuntime : options . getNativeApprovalRuntime }
286290 : { } ) ,
@@ -1731,6 +1735,83 @@ describe("server-channels auto restart", () => {
17311735 expect ( manager . getAutostartSuppression ( ) ?. reason ) . toBe ( "crash-loop-breaker" ) ;
17321736 } ) ;
17331737
1738+ it ( "recovers suppressed autostart without undoing manual stops" , async ( ) => {
1739+ const startAccount = vi . fn (
1740+ async ( { abortSignal } : ChannelGatewayContext < TestAccount > ) =>
1741+ await new Promise < void > ( ( resolve ) => {
1742+ abortSignal . addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } ) ;
1743+ } ) ,
1744+ ) ;
1745+ installTestRegistry (
1746+ createTestPlugin ( {
1747+ startAccount,
1748+ listAccountIds : ( ) => [ DEFAULT_ACCOUNT_ID , "work" ] ,
1749+ } ) ,
1750+ ) ;
1751+ const tryRecover = vi . fn ( ( ) => true ) ;
1752+ const manager = createManager ( {
1753+ tryRecoverAutostartSuppression : tryRecover ,
1754+ getRuntimeConfig : ( ) => ( {
1755+ channels : { discord : { healthMonitor : { enabled : false } } } ,
1756+ } ) ,
1757+ } ) ;
1758+ manager . setAutostartSuppression ( {
1759+ reason : "crash-loop-breaker" ,
1760+ message : "safe mode" ,
1761+ } ) ;
1762+
1763+ await manager . startChannels ( ) ;
1764+ await manager . startChannel ( "discord" , DEFAULT_ACCOUNT_ID , { manual : true } ) ;
1765+ await manager . stopChannel ( "discord" , DEFAULT_ACCOUNT_ID ) ;
1766+ await manager . recoverAutostartSuppression ( ) ;
1767+ await flushMicrotasks ( ) ;
1768+
1769+ expect ( tryRecover ) . toHaveBeenCalledOnce ( ) ;
1770+ expect ( manager . getAutostartSuppression ( ) ) . toBeNull ( ) ;
1771+ expect ( startAccount . mock . calls . map ( ( [ ctx ] ) => ctx . accountId ) ) . toEqual ( [
1772+ DEFAULT_ACCOUNT_ID ,
1773+ "work" ,
1774+ ] ) ;
1775+ expect ( manager . isHealthMonitorEnabled ( "discord" , "work" ) ) . toBe ( false ) ;
1776+ expect ( manager . isManuallyStopped ( "discord" , DEFAULT_ACCOUNT_ID ) ) . toBe ( true ) ;
1777+ } ) ;
1778+
1779+ it ( "keeps suppression when persisted recovery is not proven" , async ( ) => {
1780+ const startAccount = vi . fn ( async ( ) => { } ) ;
1781+ installTestRegistry ( createTestPlugin ( { startAccount } ) ) ;
1782+ const manager = createManager ( { tryRecoverAutostartSuppression : ( ) => false } ) ;
1783+ manager . setAutostartSuppression ( {
1784+ reason : "crash-loop-breaker" ,
1785+ message : "safe mode" ,
1786+ } ) ;
1787+
1788+ await expect ( manager . recoverAutostartSuppression ( ) ) . resolves . toBe ( false ) ;
1789+
1790+ expect ( manager . getAutostartSuppression ( ) ?. reason ) . toBe ( "crash-loop-breaker" ) ;
1791+ expect ( startAccount ) . not . toHaveBeenCalled ( ) ;
1792+ } ) ;
1793+
1794+ it ( "keeps ambient channel suppression after crash-loop recovery" , async ( ) => {
1795+ const startAccount = vi . fn ( async ( ) => { } ) ;
1796+ installTestRegistry ( createTestPlugin ( { startAccount } ) ) ;
1797+ const manager = createManager ( {
1798+ ambientAutostartSuppressedChannelIds : new Set ( [ "discord" ] ) ,
1799+ tryRecoverAutostartSuppression : ( ) => true ,
1800+ } ) ;
1801+ manager . setAutostartSuppression ( {
1802+ reason : "crash-loop-breaker" ,
1803+ message : "safe mode" ,
1804+ } ) ;
1805+
1806+ await expect ( manager . recoverAutostartSuppression ( ) ) . resolves . toBe ( true ) ;
1807+
1808+ expect ( manager . getAutostartSuppression ( ) ) . toBeNull ( ) ;
1809+ expect ( startAccount ) . not . toHaveBeenCalled ( ) ;
1810+ expect ( manager . getRuntimeSnapshot ( ) . channelAccounts . discord ?. default ?. lastError ) . toBe (
1811+ "ambient channel credentials suppressed for dev gateway" ,
1812+ ) ;
1813+ } ) ;
1814+
17341815 it ( "suppresses ambient dev channel autostart while allowing manual starts" , async ( ) => {
17351816 const startAccount = vi . fn ( async ( _ctx : ChannelGatewayContext < TestAccount > ) => { } ) ;
17361817 installTestRegistry ( createTestPlugin ( { startAccount } ) ) ;
0 commit comments