@@ -6,6 +6,7 @@ import * as FileSystem from "effect/FileSystem";
66import * as Path from "effect/Path" ;
77import * as Ref from "effect/Ref" ;
88import * as Schema from "effect/Schema" ;
9+ import * as Semaphore from "effect/Semaphore" ;
910import { HttpClient } from "effect/http" ;
1011import { ChildProcessSpawner } from "effect/process" ;
1112
@@ -24,7 +25,12 @@ import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
2425import { enrichMuseSnapshot , latestMuseVersion , museMaintenance } from "../museMaintenance.ts" ;
2526import type { MuseSubscriptionUsage } from "../museProtocol.ts" ;
2627import { makeMuseEnvironment } from "../museSdk.ts" ;
27- import { museStatusUsageLimits , museUsageWindows } from "../museUsageLimits.ts" ;
28+ import {
29+ museStatusUsageLimits ,
30+ museUsageAccount ,
31+ museUsageStillApplies ,
32+ museUsageWindows ,
33+ } from "../museUsageLimits.ts" ;
2834import type { ServerProviderDraft } from "../providerSnapshot.ts" ;
2935import {
3036 defaultProviderContinuationIdentity ,
@@ -99,14 +105,22 @@ export const MuseDriver: ProviderDriver<MuseSettings, MuseDriverEnv> = {
99105 const snapshotSettings = makeProviderSnapshotSettingsSource ( effectiveConfig , serverSettings ) ;
100106 // Meta reports subscription usage only with a model response, and a status
101107 // check makes none. Keep the newest report from any of this instance's
102- // sessions so a refresh re-derives the windows instead of dropping them.
103- const latestUsage = yield * Ref . make < MuseSubscriptionUsage | undefined > ( undefined ) ;
108+ // sessions so a refresh re-derives the windows instead of dropping them,
109+ // with the account it arrived under so a logout or another login drops it.
110+ const latestUsage = yield * Ref . make <
111+ { readonly usage : MuseSubscriptionUsage ; readonly account : string | undefined } | undefined
112+ > ( undefined ) ;
113+ // Sessions report concurrently; one at a time keeps an older report from publishing last.
114+ const usagePermit = yield * Semaphore . make ( 1 ) ;
104115 const withUsageLimits = ( provider : ServerProviderDraft ) =>
105116 Effect . gen ( function * ( ) {
117+ const retained = yield * Ref . updateAndGet ( latestUsage , ( current ) =>
118+ current && museUsageStillApplies ( current . account , provider . auth ) ? current : undefined ,
119+ ) ;
106120 const usageLimits = museStatusUsageLimits ( {
107121 auth : provider . auth ,
108122 enabled : provider . enabled ,
109- observation : yield * Ref . get ( latestUsage ) ,
123+ observation : retained ?. usage ,
110124 nowMs : DateTime . toEpochMillis ( yield * DateTime . now ) ,
111125 } ) ;
112126 return usageLimits ? { ...provider , usageLimits } : provider ;
@@ -180,23 +194,31 @@ export const MuseDriver: ProviderDriver<MuseSettings, MuseDriverEnv> = {
180194 serverConfig,
181195 fileSystem,
182196 modelCatalog,
183- latestSubscriptionUsage : Ref . get ( latestUsage ) ,
197+ latestSubscriptionUsage : Ref . get ( latestUsage ) . pipe (
198+ Effect . map ( ( retained ) => retained ?. usage ) ,
199+ ) ,
184200 onSubscriptionUsage : ( usage ) =>
185- Effect . gen ( function * ( ) {
186- // Every session's host reports; an older report arriving late must not win.
187- const newer = yield * Ref . modify ( latestUsage , ( previous ) =>
188- previous && previous . observedAtMs >= usage . observedAtMs
189- ? [ false , previous ]
190- : [ true , usage ] ,
191- ) ;
192- // An API-key instance leaves the account to its hub; see museStatusUsageLimits.
193- if ( ! newer || ( yield * snapshot . getSnapshot ) . auth . type === "apiKey" ) return ;
194- const now = yield * DateTime . now ;
195- yield * snapshot . applyUsageLimits ( {
196- windows : museUsageWindows ( usage , DateTime . toEpochMillis ( now ) ) ,
197- checkedAt : DateTime . formatIso ( DateTime . makeUnsafe ( usage . observedAtMs ) ) ,
198- } ) ;
199- } ) ,
201+ usagePermit . withPermits ( 1 ) (
202+ Effect . gen ( function * ( ) {
203+ const { auth } = yield * snapshot . getSnapshot ;
204+ // Every session's host reports; an older report arriving late must not win.
205+ const newer = yield * Ref . modify ( latestUsage , ( previous ) =>
206+ previous && previous . usage . observedAtMs >= usage . observedAtMs
207+ ? [ false , previous ]
208+ : [ true , { usage, account : museUsageAccount ( auth ) } ] ,
209+ ) ;
210+ // An API-key instance leaves the account to its hub; see museStatusUsageLimits.
211+ if ( ! newer || auth . type === "apiKey" ) return ;
212+ const now = yield * DateTime . now ;
213+ yield * snapshot . applyUsageLimits ( {
214+ windows : museUsageWindows ( usage , DateTime . toEpochMillis ( now ) ) ,
215+ checkedAt : DateTime . formatIso ( DateTime . makeUnsafe ( usage . observedAtMs ) ) ,
216+ // A report is the account's whole state, so a window that has reset
217+ // since loses its old reset time instead of keeping it.
218+ replace : true ,
219+ } ) ;
220+ } ) ,
221+ ) ,
200222 ...( eventLoggers . native ? { nativeEventLogger : eventLoggers . native } : { } ) ,
201223 continuationRequests,
202224 } ) ;
0 commit comments