Skip to content

Commit e5c5274

Browse files
ntindleclaude
andcommitted
fix(providers): address review on Muse usage limits
- A Muse report is the account's whole state, so a runtime update now replaces the published windows. A window that has reset since loses its expired reset time instead of keeping it until the next status check. - Serialize usage updates across sessions, so an older report cannot publish after a newer one. - Give account/read 2 seconds. A host that never answers leaves the account unknown instead of discarding the models the probe found. - Publish no usage while signed out, and drop the kept report on logout or when another account signs in. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 95b4fd8 commit e5c5274

9 files changed

Lines changed: 178 additions & 28 deletions

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as FileSystem from "effect/FileSystem";
66
import * as Path from "effect/Path";
77
import * as Ref from "effect/Ref";
88
import * as Schema from "effect/Schema";
9+
import * as Semaphore from "effect/Semaphore";
910
import { HttpClient } from "effect/http";
1011
import { ChildProcessSpawner } from "effect/process";
1112

@@ -24,7 +25,12 @@ import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
2425
import { enrichMuseSnapshot, latestMuseVersion, museMaintenance } from "../museMaintenance.ts";
2526
import type { MuseSubscriptionUsage } from "../museProtocol.ts";
2627
import { 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";
2834
import type { ServerProviderDraft } from "../providerSnapshot.ts";
2935
import {
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
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ export interface ServerProviderShape {
1818
/**
1919
* Fold a runtime rate-limit update into the published snapshot without
2020
* waiting for the next status probe. Sparse: windows merge by id and an
21-
* update with no usable window leaves the snapshot untouched.
21+
* update with no usable window leaves the snapshot untouched. With
22+
* `replace`, the update's windows are the whole set instead.
2223
*/
2324
readonly applyUsageLimits: (
24-
update: ProviderUsageLimitsUpdate & { readonly checkedAt: string },
25+
update: ProviderUsageLimitsUpdate & {
26+
readonly checkedAt: string;
27+
readonly replace?: boolean;
28+
},
2529
) => Effect.Effect<void>;
2630
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
190190
previous: state.snapshot.usageLimits,
191191
update,
192192
checkedAt: update.checkedAt,
193+
...(update.replace ? { replace: true } : {}),
193194
});
194195
// `applyUsageLimitsUpdate` hands back the same object when nothing
195196
// moved, which is the common case for Codex's per-tick notification.

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import { MuseSettings } from "@t3tools/contracts";
33
import * as Effect from "effect/Effect";
4+
import * as Fiber from "effect/Fiber";
45
import * as Schema from "effect/Schema";
6+
import * as TestClock from "effect/testing/TestClock";
57

68
import type { MuseSdkHost } from "./museSdk.ts";
79
import {
@@ -108,6 +110,39 @@ describe("Muse model catalog", () => {
108110
}),
109111
);
110112

113+
it.effect("keeps the models when account/read never answers", () =>
114+
Effect.gen(function* () {
115+
const base = host({
116+
providerId: "meta",
117+
models: [row("muse-spark-1.3", { isDefault: true })],
118+
});
119+
let askedForAccount = () => {};
120+
const accountRequested = new Promise<void>((resolve) => {
121+
askedForAccount = resolve;
122+
});
123+
const probing = yield* probeMuseHost(settings, {}, undefined, async () => ({
124+
...base,
125+
connection: {
126+
...base.connection,
127+
request: (method: string) => {
128+
if (method !== "account/read") return base.connection.request(method);
129+
askedForAccount();
130+
return new Promise<never>(() => {});
131+
},
132+
},
133+
})).pipe(Effect.scoped, Effect.forkChild);
134+
yield* Effect.promise(() => accountRequested);
135+
yield* Effect.yieldNow;
136+
yield* TestClock.adjust("2 seconds");
137+
// Bounded in real time, so a probe that waits on account/read fails here instead of hanging.
138+
const { models, account } = yield* TestClock.withLive(
139+
Fiber.join(probing).pipe(Effect.timeout("1 second")),
140+
);
141+
expect(models.map(({ slug }) => slug)).toEqual(["muse-spark-1.3"]);
142+
expect(account).toBeUndefined();
143+
}),
144+
);
145+
111146
it("normalizes saved and implicit efforts against the selected model before dispatch", () => {
112147
const fallback = museModelCapabilities();
113148
expect(resolveMuseReasoningEffort(fallback, undefined)).toBe("high");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ export const probeMuseHost = Effect.fn("probeMuseHost")(function* (
144144
},
145145
];
146146
});
147-
// A host without the experimental surface leaves the account unknown, as before.
147+
// A host without the experimental surface leaves the account unknown, as before,
148+
// and so does one that never answers: the models found above must not wait on it.
148149
const account = yield* Effect.tryPromise(() => host.connection.request("account/read", {})).pipe(
149150
Effect.flatMap(decodeAccountState),
151+
Effect.timeout(2_000),
150152
Effect.option,
151153
);
152154
return { models, account: Option.getOrUndefined(account) };

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { describe, expect, it } from "@effect/vitest";
33
import {
44
isMuseUsageLimitFailure,
55
museStatusUsageLimits,
6+
museUsageAccount,
67
museUsageLimitResetAt,
78
museUsageLimits,
89
museUsageObservationFromHubSignals,
10+
museUsageStillApplies,
911
museUsageWindows,
1012
} from "./museUsageLimits.ts";
1113

@@ -91,6 +93,27 @@ describe("Muse usage limits", () => {
9193
expect(status({ status: "unknown" }, true)).toEqual(museUsageLimits(observation, reported));
9294
expect(status({ status: "unknown" }, false)).toBeUndefined();
9395
expect(status(login, false, false)).toBeUndefined();
96+
// Signed out, the last report has no account to show under.
97+
expect(status({ status: "unauthenticated" }, true)).toBeUndefined();
98+
});
99+
100+
it("drops a kept report once its account logs out or another one signs in", () => {
101+
const login = {
102+
status: "authenticated",
103+
type: "accountLogin",
104+
105+
} as const;
106+
const account = museUsageAccount(login);
107+
expect(account).toBe("[email protected]");
108+
expect(museUsageStillApplies(account, login)).toBe(true);
109+
expect(museUsageStillApplies(account, { ...login, email: "[email protected]" })).toBe(false);
110+
expect(museUsageStillApplies(account, { status: "unauthenticated" })).toBe(false);
111+
// Nothing names the login without account/read, so the report stays rather than being guessed away.
112+
expect(museUsageStillApplies(account, { status: "unknown" })).toBe(true);
113+
expect(museUsageStillApplies(undefined, login)).toBe(true);
114+
expect(
115+
museUsageAccount({ status: "authenticated", type: "apiKey", label: "API key" }),
116+
).toBeUndefined();
94117
});
95118

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ 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. */
98+
export function museUsageAccount(auth: ServerProviderAuth): string | undefined {
99+
return auth.type === "accountLogin" ? (auth.email ?? auth.label) : undefined;
100+
}
101+
102+
/**
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.
106+
*/
107+
export function museUsageStillApplies(
108+
account: string | undefined,
109+
auth: ServerProviderAuth,
110+
): boolean {
111+
if (auth.status === "unauthenticated") return false;
112+
const current = museUsageAccount(auth);
113+
return account === undefined || current === undefined || account === current;
114+
}
115+
97116
/**
98117
* What a status check publishes for an instance. An API key is a gateway such
99118
* as CLIProxyAPI, or API billing, and neither names the Meta account, so a hub
@@ -114,6 +133,8 @@ export function museStatusUsageLimits(input: {
114133
message: "Muse uses an API key here. A CLIProxyAPI hub reports its own accounts.",
115134
});
116135
}
136+
// Signed out, there is no account to report on.
137+
if (input.auth.status === "unauthenticated") return undefined;
117138
if (input.observation) return museUsageLimits(input.observation, input.nowMs);
118139
if (!input.enabled || input.auth.type !== "accountLogin") return undefined;
119140
return makeUnavailableUsageLimits({

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ describe("applyUsageLimitsUpdate", () => {
6262
).toBe(published);
6363
});
6464

65+
it("replaces the windows outright when the update is the account's whole state", () => {
66+
// Muse reports a window that has reset since with no reset time; a sparse
67+
// merge would keep the expired one.
68+
const restarted = {
69+
id: "five_hour",
70+
kind: "session",
71+
label: "Session",
72+
usedPercent: 0,
73+
windowDurationMins: 300,
74+
} as const;
75+
expect(
76+
applyUsageLimitsUpdate({
77+
previous: published,
78+
checkedAt: "2026-09-03T15:00:00.000Z",
79+
update: { windows: [restarted] },
80+
replace: true,
81+
}),
82+
).toEqual({ checkedAt: "2026-09-03T15:00:00.000Z", windows: [restarted] });
83+
// The same whole state again publishes nothing new.
84+
expect(
85+
applyUsageLimitsUpdate({
86+
previous: published,
87+
checkedAt: "2026-09-03T12:00:05.000Z",
88+
update: { windows: [session, weekly] },
89+
replace: true,
90+
}),
91+
).toBe(published);
92+
});
93+
6594
it("preserves reset credits when a streamed window update changes usage", () => {
6695
const resetCredits = { availableCount: 2, nextExpiresAt: "2026-10-01T00:00:00.000Z" };
6796
const next = applyUsageLimitsUpdate({

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,44 @@ export function applyUsageLimitsUpdate(input: {
6161
readonly previous: ServerProviderUsageLimits | undefined;
6262
readonly update: ProviderUsageLimitsUpdate;
6363
readonly checkedAt: string;
64+
/**
65+
* The update is the account's whole state: windows it leaves out are
66+
* dropped, and a reset time it omits is cleared rather than kept.
67+
*/
68+
readonly replace?: boolean;
6469
}): ServerProviderUsageLimits | undefined {
6570
const { previous, update } = input;
71+
const replace = input.replace === true;
6672
if (update.windows.length === 0 || previous?.unavailable?.reason === "unsupported") {
6773
return previous;
6874
}
69-
const merged = new Map(previous?.windows.map((window) => [window.id, window] as const));
75+
const published = new Map(previous?.windows.map((window) => [window.id, window] as const));
76+
const merged = replace
77+
? new Map<ServerProviderUsageWindow["id"], ServerProviderUsageWindow>()
78+
: new Map(published);
7079
// Codex sends this notification beside every token-usage tick, almost
7180
// always with unchanged numbers. Decide "nothing changed" per window on
7281
// the way through so the no-op case never allocates a new snapshot.
73-
let changed = false;
82+
let changed = replace && update.windows.length !== published.size;
7483
for (const window of update.windows) {
75-
const existing = merged.get(window.id);
84+
const existing = published.get(window.id);
7685
const next: ServerProviderUsageWindow = {
7786
...window,
7887
usedPercent: clampPercent(window.usedPercent),
79-
...(window.resetsAt === undefined && existing?.resetsAt !== undefined
88+
...(!replace && window.resetsAt === undefined && existing?.resetsAt !== undefined
8089
? { resetsAt: existing.resetsAt }
8190
: {}),
82-
...(window.windowDurationMins === undefined && existing?.windowDurationMins !== undefined
91+
...(!replace &&
92+
window.windowDurationMins === undefined &&
93+
existing?.windowDurationMins !== undefined
8394
? { windowDurationMins: existing.windowDurationMins }
8495
: {}),
8596
};
8697
if (existing === undefined || !usageWindowEquals(existing, next)) {
8798
merged.set(window.id, next);
8899
changed = true;
100+
} else if (replace) {
101+
merged.set(window.id, existing);
89102
}
90103
}
91104
if (!changed && previous !== undefined && previous.unavailable === undefined) {

0 commit comments

Comments
 (0)