Skip to content

Commit 8af0693

Browse files
ntindleclaude
andcommitted
fix(providers): count Muse usage for the login its host started under
Muse's usage reports name no account, so the driver labeled each one with the login the snapshot named when the report arrived. A report from a host that started before a logout or another login could land under the new account, and one that arrived while signed out was kept for whoever signed in next. The driver now keeps an account generation, which a status check advances when it finds a logout or a different login. Each host reads the generation when it starts and every report carries it; only reports from the current generation are kept or published. Status checks and reports share one permit, so a check and a publish cannot interleave. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d2f3e06 commit 8af0693

5 files changed

Lines changed: 110 additions & 56 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/MuseAdapterV2.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const makeHarness = Effect.fnUntraced(function* (
207207
| "continuationRequests"
208208
| "onSubscriptionUsage"
209209
| "latestSubscriptionUsage"
210+
| "usageAccountGeneration"
210211
> = {},
211212
) {
212213
let hostCount = 0;
@@ -547,23 +548,30 @@ describe("MuseAdapterV2", () => {
547548
it.effect("hands Meta's subscription usage to the instance, with or without a turn", () =>
548549
Effect.gen(function* () {
549550
const fake = yield* makeFakeMuse();
550-
const reports: MuseSubscriptionUsage[] = [];
551+
const reports: Array<readonly [MuseSubscriptionUsage, number]> = [];
552+
let generation = 3;
551553
const harness = yield* makeHarness(
552554
fake,
553555
INSTANCE_ID,
554556
undefined,
555557
undefined,
556558
undefined,
557559
runtimePolicy,
558-
{ onSubscriptionUsage: (usage) => Effect.sync(() => void reports.push(usage)) },
560+
{
561+
onSubscriptionUsage: (usage, accountGeneration) =>
562+
Effect.sync(() => void reports.push([usage, accountGeneration])),
563+
usageAccountGeneration: Effect.sync(() => generation),
564+
},
559565
);
566+
// A report counts for the generation its host started under, not the current one.
567+
generation = 4;
560568
yield* fake.emitHostWide("usage/changed", SUBSCRIPTION_USAGE);
561569
yield* fake.emitHostWide("usage/changed", { window: "not usage" });
562570
const { nativeId } = yield* startConversation(harness, fake);
563571
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
564572
const terminal = yield* harness.takeEvent("turn.terminal");
565573
assert.strictEqual(terminal.status, "completed");
566-
assert.deepStrictEqual(reports, [SUBSCRIPTION_USAGE]);
574+
assert.deepStrictEqual(reports, [[SUBSCRIPTION_USAGE, 3]]);
567575
assert.strictEqual(fake.closeCount(), 0);
568576
}).pipe(Effect.scoped, Effect.provide(testLayer)),
569577
);

‎apps/server/src/orchestration-v2/Adapters/MuseAdapterV2.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,16 @@ export interface MuseAdapterV2Options {
218218
request: ProviderContinuationRequests.ProviderContinuationRequest,
219219
) => Effect.Effect<void>;
220220
};
221-
/** Receives each subscription usage report (`usage/changed`) from this adapter's hosts. */
222-
readonly onSubscriptionUsage?: (usage: MuseSubscriptionUsage) => Effect.Effect<void>;
221+
/**
222+
* Receives each subscription usage report (`usage/changed`) from this adapter's hosts,
223+
* with the account generation the reporting host started under.
224+
*/
225+
readonly onSubscriptionUsage?: (
226+
usage: MuseSubscriptionUsage,
227+
accountGeneration: number,
228+
) => Effect.Effect<void>;
229+
/** The instance's account generation, read as each host starts, since reports name no account. */
230+
readonly usageAccountGeneration?: Effect.Effect<number>;
223231
/** The newest report across the instance, for dating a turn stopped at a usage limit. */
224232
readonly latestSubscriptionUsage?: Effect.Effect<MuseSubscriptionUsage | undefined>;
225233
}
@@ -326,6 +334,8 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
326334
const eventPermit = yield* Semaphore.make(1);
327335
let host: MuseSdkHost;
328336
let hostEpoch = 0;
337+
// The account generation the current host started under; see usageAccountGeneration.
338+
let hostAccountGeneration = 0;
329339
let closed = false;
330340
let broken = false;
331341
let nativeSessionId: string | undefined;
@@ -946,7 +956,8 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
946956
// Account-wide, so it names no session: Meta's subscription windows after a response.
947957
if (method === "usage/changed") {
948958
const usage = yield* decode(MuseSubscriptionUsage, params);
949-
if (options.onSubscriptionUsage) yield* options.onSubscriptionUsage(usage);
959+
if (options.onSubscriptionUsage)
960+
yield* options.onSubscriptionUsage(usage, hostAccountGeneration);
950961
return;
951962
}
952963
if (params.sessionId !== nativeSessionId) return;
@@ -1342,6 +1353,9 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
13421353
);
13431354
const launchHost = Effect.fnUntraced(function* () {
13441355
const epoch = ++hostEpoch;
1356+
hostAccountGeneration = options.usageAccountGeneration
1357+
? yield* options.usageAccountGeneration
1358+
: 0;
13451359
const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
13461360
const created = yield* Effect.acquireRelease(
13471361
createMuseSdkHostEffect(

‎apps/server/src/provider/Drivers/MuseDriver.ts‎

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import type { MuseSubscriptionUsage } from "../museProtocol.ts";
2727
import { makeMuseEnvironment } from "../museSdk.ts";
2828
import {
2929
museStatusUsageLimits,
30-
museUsageAccount,
31-
museUsageStillApplies,
3230
museUsageWindows,
31+
nextMuseUsageAccount,
32+
type MuseUsageAccount,
3333
} from "../museUsageLimits.ts";
3434
import type { ServerProviderDraft } from "../providerSnapshot.ts";
3535
import {
@@ -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)),

‎apps/server/src/provider/museUsageLimits.test.ts‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
museUsageLimitResetAt,
88
museUsageLimits,
99
museUsageObservationFromHubSignals,
10-
museUsageStillApplies,
1110
museUsageWindows,
11+
nextMuseUsageAccount,
1212
} from "./museUsageLimits.ts";
1313

1414
// What Meta reported after a reply on 2026-10-08, as Muse forwards it.
@@ -108,23 +108,31 @@ describe("Muse usage limits", () => {
108108
).toBeUndefined();
109109
});
110110

111-
it("drops a kept report once its account logs out or another one signs in", () => {
111+
it("starts a new account generation on a logout or another login", () => {
112112
const login = {
113113
status: "authenticated",
114114
type: "accountLogin",
115115
116116
} as const;
117-
const account = museUsageAccount(login);
118-
expect(account).toBe("[email protected]");
119-
expect(museUsageStillApplies(account, login)).toBe(true);
120-
expect(museUsageStillApplies(account, { ...login, email: "[email protected]" })).toBe(false);
121-
expect(museUsageStillApplies(account, { status: "unauthenticated" })).toBe(false);
122-
// Nothing names the login without account/read, so the report stays rather than being guessed away.
123-
expect(museUsageStillApplies(account, { status: "unknown" })).toBe(true);
124-
expect(museUsageStillApplies(undefined, login)).toBe(true);
125-
expect(
126-
museUsageAccount({ status: "authenticated", type: "apiKey", label: "API key" }),
127-
).toBeUndefined();
117+
const apiKey = { status: "authenticated", type: "apiKey", label: "API key" } as const;
118+
// The first login a check names is the one the hosts so far started under.
119+
const named = nextMuseUsageAccount({ generation: 0, identity: undefined }, login);
120+
expect(named).toEqual({ generation: 0, identity: "[email protected]" });
121+
expect(nextMuseUsageAccount(named, login)).toBe(named);
122+
// Nothing names the login without account/read, and an API key names no account.
123+
expect(nextMuseUsageAccount(named, { status: "unknown" })).toBe(named);
124+
expect(nextMuseUsageAccount(named, apiKey)).toBe(named);
125+
expect(museUsageAccount(apiKey)).toBeUndefined();
126+
const other = nextMuseUsageAccount(named, { ...login, email: "[email protected]" });
127+
expect(other).toEqual({ generation: 1, identity: "[email protected]" });
128+
const signedOut = nextMuseUsageAccount(other, { status: "unauthenticated" });
129+
expect(signedOut).toEqual({ generation: 2, identity: null });
130+
// Signing in again, even as the same account, starts another: a host from before the
131+
// logout may hold either login.
132+
expect(nextMuseUsageAccount(signedOut, { ...login, email: "[email protected]" })).toEqual({
133+
generation: 3,
134+
identity: "[email protected]",
135+
});
128136
});
129137

130138
it("recognises Muse's wording for a Meta quota refusal only", () => {

‎apps/server/src/provider/museUsageLimits.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,32 @@ export function museUsageLimits(
9494
});
9595
}
9696

97-
/** The account a report belongs to: the login's address or label, when a status check named one. */
97+
/** The login a status check names: its address or label. */
9898
export function museUsageAccount(auth: ServerProviderAuth): string | undefined {
9999
return auth.type === "accountLogin" ? (auth.email ?? auth.label) : undefined;
100100
}
101101

102102
/**
103-
* Whether a report that arrived under `account` still belongs to the login a
104-
* status check found. A logout or another account's login drops it, so a
105-
* report never shows under an account it did not come from.
103+
* Which login an instance's usage reports belong to. Reports name no account,
104+
* so each one counts for the generation its host started under, and a logout
105+
* or another login starts a new generation: a report from an older host is
106+
* never shown under the account that replaced it.
106107
*/
107-
export function museUsageStillApplies(
108-
account: string | undefined,
108+
export interface MuseUsageAccount {
109+
readonly generation: number;
110+
/** The login last named, `null` once signed out, `undefined` before any status check names one. */
111+
readonly identity: string | null | undefined;
112+
}
113+
114+
export function nextMuseUsageAccount(
115+
account: MuseUsageAccount,
109116
auth: ServerProviderAuth,
110-
): boolean {
111-
if (auth.status === "unauthenticated") return false;
112-
const current = museUsageAccount(auth);
113-
return account === undefined || current === undefined || account === current;
117+
): MuseUsageAccount {
118+
const identity = auth.status === "unauthenticated" ? null : museUsageAccount(auth);
119+
if (identity === undefined || identity === account.identity) return account;
120+
// The first login a check names is the one the hosts so far started under.
121+
if (account.identity === undefined) return { ...account, identity };
122+
return { generation: account.generation + 1, identity };
114123
}
115124

116125
/**
@@ -123,7 +132,7 @@ export function museStatusUsageLimits(input: {
123132
readonly auth: ServerProviderAuth;
124133
readonly enabled: boolean;
125134
readonly observation: MuseUsageObservation | undefined;
126-
/** This check dropped the kept report, for a logout or another account's login. */
135+
/** This check dropped the kept report: a logout or another login started a new generation. */
127136
readonly dropped?: boolean;
128137
readonly nowMs: number;
129138
}): ServerProviderUsageLimits | undefined {

0 commit comments

Comments
 (0)