@@ -2,7 +2,11 @@ import { assert, describe, it } from "@effect/vitest";
22import {
33 CONTRIBUTION_STATUS_MAX_ITEMS ,
44 CONTRIBUTION_STATUS_MAX_ITEMS_PER_SOURCE ,
5+ CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS ,
6+ CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD ,
7+ CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS ,
58 CONTRIBUTION_STATUS_MAX_THREADS ,
9+ type ContributionStatusSnapshot ,
610 type ContributionStatusSource ,
711 ProviderDriverKind ,
812 ProviderInstanceId ,
@@ -38,6 +42,27 @@ const openHandle = Effect.fn("openHandle")(function* (
3842 return { handle, close : Scope . close ( scope , Exit . void ) } ;
3943} ) ;
4044
45+ /** A plugin producer, which has its own capacity pool beside provider sessions. */
46+ const openPlugin = Effect . fn ( "openPlugin" ) ( function * (
47+ store : ContributionStatusStore . ContributionStatusStoreShape ,
48+ pluginId : string ,
49+ threadId : ThreadId ,
50+ ) {
51+ const handle = yield * store
52+ . openSource ( { kind : "plugin" , pluginId, name : pluginId } )
53+ . pipe ( Scope . provide ( yield * Scope . make ( ) ) ) ;
54+ yield * handle . bindThread ( threadId ) ;
55+ return handle ;
56+ } ) ;
57+
58+ const countBy = ( snapshot : ContributionStatusSnapshot , kind : ContributionStatusSource [ "kind" ] ) => {
59+ const entries = snapshot . entries . filter ( ( entry ) => entry . source . kind === kind ) ;
60+ return {
61+ threads : new Set ( entries . map ( ( entry ) => entry . threadId ) ) . size ,
62+ items : entries . reduce ( ( total , entry ) => total + entry . items . length , 0 ) ,
63+ } ;
64+ } ;
65+
4166const itemsByThread = ( store : ContributionStatusStore . ContributionStatusStoreShape ) =>
4267 Effect . map ( store . snapshot , ( snapshot ) =>
4368 Object . fromEntries (
@@ -169,10 +194,9 @@ describe("ContributionStatusStore", () => {
169194
170195 const snapshot = yield * store . snapshot ;
171196 assert . deepStrictEqual ( yield * itemsByThread ( store ) , { [ THREAD_A ] : [ "mode=new" ] } ) ;
172- assert . strictEqual (
173- snapshot . entries [ 0 ] ?. source . providerSessionId ,
174- ProviderSessionId . make ( "session-new" ) ,
175- ) ;
197+ assert . deepInclude ( snapshot . entries [ 0 ] ?. source , {
198+ providerSessionId : ProviderSessionId . make ( "session-new" ) ,
199+ } ) ;
176200 } ) ,
177201 ) ;
178202
@@ -277,4 +301,75 @@ describe("ContributionStatusStore", () => {
277301 ) ;
278302 } ) ,
279303 ) ;
304+ it . effect ( "keeps every slot a provider session had on a thread full of plugins" , ( ) =>
305+ Effect . gen ( function * ( ) {
306+ const store = yield * ContributionStatusStore . make ( ) ;
307+ for ( let index = 0 ; index <= CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD ; index += 1 ) {
308+ const plugin = yield * openPlugin ( store , `acme.p${ index } ` , THREAD_A ) ;
309+ yield * plugin . set ( { key : "run" , text : "on" } ) ;
310+ }
311+ // The plugin pool is full on this thread: the extra plugin was refused.
312+ assert . strictEqual (
313+ countBy ( yield * store . snapshot , "plugin" ) . items ,
314+ CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD ,
315+ ) ;
316+
317+ const provider = yield * openHandle ( store , "session-1" , THREAD_A ) ;
318+ yield * provider . handle . set ( { key : "mode" , text : "plan" } ) ;
319+ const kinds = ( yield * store . snapshot ) . entries . map ( ( entry ) => entry . source . kind ) ;
320+ assert . deepStrictEqual ( kinds , [ "provider-session" , "plugin" , "plugin" , "plugin" ] ) ;
321+ } ) ,
322+ ) ;
323+
324+ it . effect ( "never lets plugins take thread or item capacity from provider sessions" , ( ) =>
325+ Effect . gen ( function * ( ) {
326+ const store = yield * ContributionStatusStore . make ( ) ;
327+ // A provider status that exists before the plugins arrive.
328+ const existing = yield * openHandle ( store , "provider-0" , ThreadId . make ( "t-0" ) ) ;
329+ yield * existing . handle . set ( { key : "mode" , text : "plan" } ) ;
330+
331+ // Plugins fill their whole pool: every plugin thread and every plugin item.
332+ const perThread =
333+ CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS / CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS ;
334+ for ( let index = 0 ; index < CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS ; index += 1 ) {
335+ const plugin = yield * openPlugin ( store , `acme.p${ index } ` , ThreadId . make ( `t-${ index } ` ) ) ;
336+ for ( let item = 0 ; item < perThread ; item += 1 )
337+ yield * plugin . set ( { key : `k${ item } ` , text : "on" } ) ;
338+ }
339+ const refused = yield * openPlugin ( store , "acme.late" , ThreadId . make ( "t-late" ) ) ;
340+ yield * refused . set ( { key : "k" , text : "refused" } ) ;
341+ assert . deepStrictEqual ( countBy ( yield * store . snapshot , "plugin" ) , {
342+ threads : CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS ,
343+ items : CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS ,
344+ } ) ;
345+
346+ // The existing provider source still adds a new key, and providers still
347+ // reach their own thread and item caps, on plugin threads and new ones.
348+ yield * existing . handle . set ( { key : "branch" , text : "main" } ) ;
349+ const perProvider = CONTRIBUTION_STATUS_MAX_ITEMS / CONTRIBUTION_STATUS_MAX_THREADS ;
350+ for ( let item = 2 ; item < perProvider ; item += 1 )
351+ yield * existing . handle . set ( { key : `k${ item } ` , text : "on" } ) ;
352+ for ( let index = 1 ; index < CONTRIBUTION_STATUS_MAX_THREADS ; index += 1 ) {
353+ const provider = yield * openHandle ( store , `provider-${ index } ` , ThreadId . make ( `t-${ index } ` ) ) ;
354+ for ( let item = 0 ; item < perProvider ; item += 1 )
355+ yield * provider . handle . set ( { key : `k${ item } ` , text : "on" } ) ;
356+ }
357+ const snapshot = yield * store . snapshot ;
358+ assert . deepStrictEqual ( countBy ( snapshot , "provider-session" ) , {
359+ threads : CONTRIBUTION_STATUS_MAX_THREADS ,
360+ items : CONTRIBUTION_STATUS_MAX_ITEMS ,
361+ } ) ;
362+ assert . deepStrictEqual ( countBy ( snapshot , "plugin" ) , {
363+ threads : CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS ,
364+ items : CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS ,
365+ } ) ;
366+ // The provider caps themselves are unchanged.
367+ const overflow = yield * openHandle ( store , "provider-late" , ThreadId . make ( "t-late" ) ) ;
368+ yield * overflow . handle . set ( { key : "mode" , text : "refused" } ) ;
369+ assert . strictEqual (
370+ countBy ( yield * store . snapshot , "provider-session" ) . items ,
371+ CONTRIBUTION_STATUS_MAX_ITEMS ,
372+ ) ;
373+ } ) ,
374+ ) ;
280375} ) ;
0 commit comments