@@ -26,6 +26,11 @@ import { createEmptyPluginRegistry, type PluginRegistry } from "../plugins/regis
2626import { getActivePluginRegistry , setActivePluginRegistry } from "../plugins/runtime.js" ;
2727import { createRuntimeChannel } from "../plugins/runtime/runtime-channel.js" ;
2828import type { PluginRuntime } from "../plugins/runtime/types.js" ;
29+ import {
30+ isGatewaySubordinateWorkAdmissionClosed ,
31+ resetGatewayWorkAdmission ,
32+ tryBeginGatewayRootWorkAdmission ,
33+ } from "../process/gateway-work-admission.js" ;
2934import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js" ;
3035import type { RuntimeEnv } from "../runtime.js" ;
3136import {
@@ -303,6 +308,7 @@ describe("server-channels auto restart", () => {
303308 let previousRegistry : PluginRegistry | null = null ;
304309
305310 beforeEach ( ( ) => {
311+ resetGatewayWorkAdmission ( ) ;
306312 previousRegistry = getActivePluginRegistry ( ) ;
307313 vi . useRealTimers ( ) ;
308314 vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" , "Date" ] } ) ;
@@ -323,10 +329,136 @@ describe("server-channels auto restart", () => {
323329 await flushMicrotasks ( ) ;
324330 vi . clearAllTimers ( ) ;
325331 vi . useRealTimers ( ) ;
332+ resetGatewayWorkAdmission ( ) ;
326333 setActiveDegradedSecretOwners ( [ ] ) ;
327334 setActivePluginRegistry ( previousRegistry ?? createEmptyPluginRegistry ( ) ) ;
328335 } ) ;
329336
337+ it ( "keeps a channel task admitted after the starting request finishes" , async ( ) => {
338+ const continueChannelTask = createDeferred ( ) ;
339+ const observedAdmission = createDeferred < boolean > ( ) ;
340+ const startAccount = vi . fn ( async ( { abortSignal } : ChannelGatewayContext < TestAccount > ) => {
341+ await continueChannelTask . promise ;
342+ observedAdmission . resolve ( isGatewaySubordinateWorkAdmissionClosed ( ) ) ;
343+ await new Promise < void > ( ( resolve ) => {
344+ abortSignal . addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } ) ;
345+ } ) ;
346+ } ) ;
347+ installTestRegistry ( createTestPlugin ( { startAccount } ) ) ;
348+ const manager = createManager ( ) ;
349+ const requestAdmission = tryBeginGatewayRootWorkAdmission ( ) ;
350+ expect ( requestAdmission ) . not . toBeNull ( ) ;
351+ if ( ! requestAdmission ) {
352+ return ;
353+ }
354+
355+ try {
356+ await requestAdmission . run ( async ( ) => {
357+ await manager . startChannel ( "discord" , DEFAULT_ACCOUNT_ID , { manual : true } ) ;
358+ await waitForImmediate ( ) ;
359+ await waitForMicrotaskCondition (
360+ ( ) => startAccount . mock . calls . length === 1 ,
361+ "expected channel task to start" ,
362+ ) ;
363+ } ) ;
364+ requestAdmission . release ( ) ;
365+ continueChannelTask . resolve ( ) ;
366+
367+ await expect ( observedAdmission . promise ) . resolves . toBe ( false ) ;
368+ } finally {
369+ requestAdmission . release ( ) ;
370+ continueChannelTask . resolve ( ) ;
371+ }
372+ } ) ;
373+
374+ it ( "keeps approval-bootstrap descendants admitted after the starting request finishes" , async ( ) => {
375+ const continueApprovalDescendant = createDeferred ( ) ;
376+ const observedAdmission = createDeferred < boolean > ( ) ;
377+ hoisted . startChannelApprovalHandlerBootstrap . mockImplementation ( async ( ) => {
378+ void Promise . resolve ( ) . then ( async ( ) => {
379+ await continueApprovalDescendant . promise ;
380+ observedAdmission . resolve ( isGatewaySubordinateWorkAdmissionClosed ( ) ) ;
381+ } ) ;
382+ return async ( ) => { } ;
383+ } ) ;
384+ const startAccount = vi . fn (
385+ async ( { abortSignal } : ChannelGatewayContext < TestAccount > ) =>
386+ await new Promise < void > ( ( resolve ) => {
387+ abortSignal . addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } ) ;
388+ } ) ,
389+ ) ;
390+ installTestRegistry ( createTestPlugin ( { startAccount } ) ) ;
391+ const manager = createManager ( ) ;
392+ const requestAdmission = tryBeginGatewayRootWorkAdmission ( ) ;
393+ expect ( requestAdmission ) . not . toBeNull ( ) ;
394+ if ( ! requestAdmission ) {
395+ return ;
396+ }
397+
398+ try {
399+ await requestAdmission . run ( async ( ) => {
400+ await manager . startChannel ( "discord" , DEFAULT_ACCOUNT_ID , { manual : true } ) ;
401+ await waitForImmediate ( ) ;
402+ await waitForMicrotaskCondition (
403+ ( ) => startAccount . mock . calls . length === 1 ,
404+ "expected channel task to start" ,
405+ ) ;
406+ } ) ;
407+ requestAdmission . release ( ) ;
408+ continueApprovalDescendant . resolve ( ) ;
409+
410+ await expect ( observedAdmission . promise ) . resolves . toBe ( false ) ;
411+ } finally {
412+ requestAdmission . release ( ) ;
413+ continueApprovalDescendant . resolve ( ) ;
414+ }
415+ } ) ;
416+
417+ it ( "keeps automatic restarts admitted after the starting request finishes" , async ( ) => {
418+ const finishFirstChannelTask = createDeferred ( ) ;
419+ const observedAdmission : boolean [ ] = [ ] ;
420+ const startAccount = vi . fn ( async ( { abortSignal } : ChannelGatewayContext < TestAccount > ) => {
421+ observedAdmission . push ( isGatewaySubordinateWorkAdmissionClosed ( ) ) ;
422+ if ( observedAdmission . length === 1 ) {
423+ await finishFirstChannelTask . promise ;
424+ return ;
425+ }
426+ await new Promise < void > ( ( resolve ) => {
427+ abortSignal . addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } ) ;
428+ } ) ;
429+ } ) ;
430+ installTestRegistry ( createTestPlugin ( { startAccount } ) ) ;
431+ const manager = createManager ( ) ;
432+ const requestAdmission = tryBeginGatewayRootWorkAdmission ( ) ;
433+ expect ( requestAdmission ) . not . toBeNull ( ) ;
434+ if ( ! requestAdmission ) {
435+ return ;
436+ }
437+
438+ try {
439+ await requestAdmission . run ( async ( ) => {
440+ await manager . startChannel ( "discord" , DEFAULT_ACCOUNT_ID , { manual : true } ) ;
441+ await waitForImmediate ( ) ;
442+ await waitForMicrotaskCondition (
443+ ( ) => startAccount . mock . calls . length === 1 ,
444+ "expected initial channel task to start" ,
445+ ) ;
446+ } ) ;
447+ requestAdmission . release ( ) ;
448+ finishFirstChannelTask . resolve ( ) ;
449+ await advanceTimersUntil (
450+ ( ) => startAccount . mock . calls . length === 2 ,
451+ "expected channel task to restart" ,
452+ { stepMs : 10 , maxMs : 100 } ,
453+ ) ;
454+
455+ expect ( observedAdmission ) . toEqual ( [ false , false ] ) ;
456+ } finally {
457+ requestAdmission . release ( ) ;
458+ finishFirstChannelTask . resolve ( ) ;
459+ }
460+ } ) ;
461+
330462 it ( "caps crash-loop restarts after max attempts" , async ( ) => {
331463 const startAccount = vi . fn ( async ( ) => { } ) ;
332464 installTestRegistry (
0 commit comments