@@ -27,9 +27,9 @@ import type { MuseSubscriptionUsage } from "../museProtocol.ts";
2727import { makeMuseEnvironment } from "../museSdk.ts" ;
2828import {
2929 museStatusUsageLimits ,
30- museUsageAccount ,
31- museUsageStillApplies ,
3230 museUsageWindows ,
31+ nextMuseUsageAccount ,
32+ type MuseUsageAccount ,
3333} from "../museUsageLimits.ts" ;
3434import type { ServerProviderDraft } from "../providerSnapshot.ts" ;
3535import {
@@ -105,30 +105,39 @@ export const MuseDriver: ProviderDriver<MuseSettings, MuseDriverEnv> = {
105105 const snapshotSettings = makeProviderSnapshotSettingsSource ( effectiveConfig , serverSettings ) ;
106106 // Meta reports subscription usage only with a model response, and a status
107107 // check makes none. Keep the newest report from any of this instance's
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.
108+ // sessions so a refresh re-derives the windows instead of dropping them.
109+ // Each report counts for the account generation its host started under.
110+ const usageAccount = yield * Ref . make < MuseUsageAccount > ( {
111+ generation : 0 ,
112+ identity : undefined ,
113+ } ) ;
110114 const latestUsage = yield * Ref . make <
111- { readonly usage : MuseSubscriptionUsage ; readonly account : string | undefined } | undefined
115+ { readonly usage : MuseSubscriptionUsage ; readonly generation : number } | undefined
112116 > ( undefined ) ;
113- // Sessions report concurrently; one at a time keeps an older report from publishing last.
117+ // Reports and status checks take turns, so an older report cannot publish after a
118+ // newer one, and none publishes for a generation a check has just replaced.
114119 const usagePermit = yield * Semaphore . make ( 1 ) ;
115120 const withUsageLimits = ( provider : ServerProviderDraft ) =>
116- Effect . gen ( function * ( ) {
117- const { retained, dropped } = yield * Ref . modify ( latestUsage , ( current ) => {
118- const drop =
119- current !== undefined && ! museUsageStillApplies ( current . account , provider . auth ) ;
120- const kept = drop ? undefined : current ;
121- return [ { retained : kept , dropped : drop } , kept ] as const ;
122- } ) ;
123- const usageLimits = museStatusUsageLimits ( {
124- auth : provider . auth ,
125- enabled : provider . enabled ,
126- observation : retained ?. usage ,
127- dropped,
128- nowMs : DateTime . toEpochMillis ( yield * DateTime . now ) ,
129- } ) ;
130- return usageLimits ? { ...provider , usageLimits } : provider ;
131- } ) ;
121+ usagePermit . withPermits ( 1 ) (
122+ Effect . gen ( function * ( ) {
123+ const { generation } = yield * Ref . updateAndGet ( usageAccount , ( account ) =>
124+ nextMuseUsageAccount ( account , provider . auth ) ,
125+ ) ;
126+ const { retained, dropped } = yield * Ref . modify ( latestUsage , ( current ) => {
127+ const drop = current !== undefined && current . generation !== generation ;
128+ const kept = drop ? undefined : current ;
129+ return [ { retained : kept , dropped : drop } , kept ] as const ;
130+ } ) ;
131+ const usageLimits = museStatusUsageLimits ( {
132+ auth : provider . auth ,
133+ enabled : provider . enabled ,
134+ observation : retained ?. usage ,
135+ dropped,
136+ nowMs : DateTime . toEpochMillis ( yield * DateTime . now ) ,
137+ } ) ;
138+ return usageLimits ? { ...provider , usageLimits } : provider ;
139+ } ) ,
140+ ) ;
132141 const resolveInstallation = yield * makeCachedProviderMaintenanceResolution (
133142 resolveProviderMaintenanceCapabilitiesEffect ( museMaintenance , {
134143 binaryPath : effectiveConfig . binaryPath ,
@@ -201,18 +210,24 @@ export const MuseDriver: ProviderDriver<MuseSettings, MuseDriverEnv> = {
201210 latestSubscriptionUsage : Ref . get ( latestUsage ) . pipe (
202211 Effect . map ( ( retained ) => retained ?. usage ) ,
203212 ) ,
204- onSubscriptionUsage : ( usage ) =>
213+ usageAccountGeneration : Ref . get ( usageAccount ) . pipe (
214+ Effect . map ( ( account ) => account . generation ) ,
215+ ) ,
216+ onSubscriptionUsage : ( usage , hostGeneration ) =>
205217 usagePermit . withPermits ( 1 ) (
206218 Effect . gen ( function * ( ) {
207- const { auth } = yield * snapshot . getSnapshot ;
219+ const account = yield * Ref . get ( usageAccount ) ;
220+ // A host from before a logout or another login reports for an account
221+ // that is gone, and signed out there is no account to report for.
222+ if ( hostGeneration !== account . generation || account . identity === null ) return ;
208223 // Every session's host reports; an older report arriving late must not win.
209224 const newer = yield * Ref . modify ( latestUsage , ( previous ) =>
210225 previous && previous . usage . observedAtMs >= usage . observedAtMs
211226 ? [ false , previous ]
212- : [ true , { usage, account : museUsageAccount ( auth ) } ] ,
227+ : [ true , { usage, generation : hostGeneration } ] ,
213228 ) ;
214229 // An API-key instance leaves the account to its hub; see museStatusUsageLimits.
215- if ( ! newer || auth . type === "apiKey" ) return ;
230+ if ( ! newer || ( yield * snapshot . getSnapshot ) . auth . type === "apiKey" ) return ;
216231 const now = yield * DateTime . now ;
217232 yield * snapshot . applyUsageLimits ( {
218233 windows : museUsageWindows ( usage , DateTime . toEpochMillis ( now ) ) ,
0 commit comments