Skip to content

Commit e7d1bc3

Browse files
committed
fix(muse): simplify limits replacement and isolate credential changes
1 parent be87f34 commit e7d1bc3

6 files changed

Lines changed: 57 additions & 64 deletions

File tree

‎packages/provider-core/src/server/usageLimits.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ describe("applyUsageLimitsUpdate", () => {
7777
).toEqual(published);
7878
});
7979

80+
it("clears all windows when a whole-state report is empty", () => {
81+
expect(
82+
applyUsageLimitsUpdate({
83+
previous: published,
84+
checkedAt,
85+
update: { windows: [] },
86+
replace: true,
87+
}),
88+
).toEqual({ checkedAt, windows: [] });
89+
});
90+
8091
it("clears a failed probe on replacement even when windows and time are unchanged", () => {
8192
const previous = {
8293
...published,

‎packages/provider-core/src/server/usageLimits.ts‎

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,58 +51,57 @@ export function applyUsageLimitsUpdate(input: {
5151
readonly previous: ServerProviderUsageLimits | undefined;
5252
readonly update: ProviderUsageLimitsUpdate;
5353
readonly checkedAt: string;
54-
/**
55-
* The update is the account's whole state: windows it leaves out are
56-
* dropped, and a reset time it omits is cleared rather than kept.
57-
*/
54+
/** Replace the whole window set, including omitted resets and windows. */
5855
readonly replace?: boolean;
5956
}): ServerProviderUsageLimits | undefined {
6057
const { previous, update } = input;
61-
const replace = input.replace === true;
62-
if (update.windows.length === 0 || previous?.unavailable?.reason === "unsupported") {
58+
if (previous?.unavailable?.reason === "unsupported") return previous;
59+
if (input.replace) {
60+
const next = makeUsageLimits({
61+
checkedAt: input.checkedAt,
62+
windows: update.windows.map((window) => ({
63+
...window,
64+
usedPercent: clampPercent(window.usedPercent),
65+
})),
66+
});
67+
if (
68+
previous &&
69+
!previous.unavailable &&
70+
previous.checkedAt === next.checkedAt &&
71+
previous.windows.length === next.windows.length &&
72+
previous.windows.every((window, index) => usageWindowEquals(window, next.windows[index]!))
73+
)
74+
return previous;
75+
return { ...next, ...(previous?.resetCredits ? { resetCredits: previous.resetCredits } : {}) };
76+
}
77+
if (update.windows.length === 0) {
6378
return previous;
6479
}
65-
const published = new Map(previous?.windows.map((window) => [window.id, window] as const));
66-
const merged = replace
67-
? new Map<ServerProviderUsageWindow["id"], ServerProviderUsageWindow>()
68-
: new Map(published);
69-
// Decide "nothing changed" per window so a no-op never allocates a new
70-
// snapshot. A whole-state report that only moves the time is still news:
71-
// Limits picks the freshest report per account by `checkedAt`.
72-
let changed =
73-
replace &&
74-
(update.windows.length !== published.size ||
75-
input.checkedAt !== previous?.checkedAt ||
76-
previous?.unavailable !== undefined);
80+
const merged = new Map(previous?.windows.map((window) => [window.id, window] as const));
81+
// Codex repeats unchanged limits on token ticks; avoid allocating snapshots for no-ops.
82+
let changed = false;
7783
for (const window of update.windows) {
78-
const existing = published.get(window.id);
84+
const existing = merged.get(window.id);
7985
const next: ServerProviderUsageWindow = {
8086
...window,
8187
usedPercent: clampPercent(window.usedPercent),
82-
...(!replace && window.resetsAt === undefined && existing?.resetsAt !== undefined
88+
...(window.resetsAt === undefined && existing?.resetsAt !== undefined
8389
? { resetsAt: existing.resetsAt }
8490
: {}),
85-
...(!replace &&
86-
window.windowDurationMins === undefined &&
87-
existing?.windowDurationMins !== undefined
91+
...(window.windowDurationMins === undefined && existing?.windowDurationMins !== undefined
8892
? { windowDurationMins: existing.windowDurationMins }
8993
: {}),
9094
};
9195
if (existing === undefined || !usageWindowEquals(existing, next)) {
9296
merged.set(window.id, next);
9397
changed = true;
94-
} else if (replace) {
95-
merged.set(window.id, existing);
9698
}
9799
}
98100
if (!changed && previous !== undefined) {
99101
return previous;
100102
}
101-
const unavailable = replace
102-
? undefined
103-
: previous === undefined
104-
? { reason: "probeFailed" as const }
105-
: previous.unavailable;
103+
const unavailable =
104+
previous === undefined ? { reason: "probeFailed" as const } : previous.unavailable;
106105
return {
107106
...makeUsageLimits({ checkedAt: input.checkedAt, windows: merged.values() }),
108107
...(previous?.resetCredits !== undefined ? { resetCredits: previous.resetCredits } : {}),

‎packages/provider-muse/src/server/driver.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { makeMuseEnvironment } from "./sdk.ts";
2828
import type { MuseSubscriptionUsage } from "./protocol.ts";
2929
import {
3030
museStatusUsageLimits,
31+
museUsageAccount,
3132
museUsageWindows,
3233
nextMuseUsageAccount,
3334
type MuseUsageAccount,
@@ -245,8 +246,10 @@ export const MuseDriver: ProviderDriver<
245246
? [false, previous]
246247
: [true, { usage, generation: hostGeneration }],
247248
);
248-
// An API key names no account; see museStatusUsageLimits.
249-
if (!newer || (yield* snapshot.getSnapshot).auth.type === "apiKey") return;
249+
const { auth } = yield* snapshot.getSnapshot;
250+
// The probe may have advanced the generation before publishing its new account.
251+
if (!newer || auth.type === "apiKey" || museUsageAccount(auth) !== account.identity)
252+
return;
250253
const now = yield* DateTime.now;
251254
yield* snapshot.applyUsageLimits({
252255
windows: museUsageWindows(usage, DateTime.toEpochMillis(now)),

‎packages/provider-muse/src/server/modelCatalog.test.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,8 @@ describe("Muse model catalog", () => {
132132
},
133133
})).pipe(Effect.scoped, Effect.forkChild);
134134
yield* Effect.promise(() => accountRequested);
135-
yield* Effect.yieldNow;
136135
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-
);
136+
const { models, account } = yield* Fiber.join(probing);
141137
expect(models.map(({ slug }) => slug)).toEqual(["muse-spark-1.3"]);
142138
expect(account).toBeUndefined();
143139
}),

‎packages/provider-muse/src/server/usageLimits.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ describe("Muse usage limits", () => {
173173
expect(nextMuseUsageAccount(named, login)).toBe(named);
174174
// Nothing names the login without account/read, and an API key names no account.
175175
expect(nextMuseUsageAccount(named, { status: "unknown" })).toBe(named);
176-
expect(nextMuseUsageAccount(named, apiKey)).toBe(named);
176+
const keyAccount = nextMuseUsageAccount(named, apiKey);
177+
expect(keyAccount).toEqual({ generation: 1, identity: null });
178+
expect(nextMuseUsageAccount(keyAccount, login)).toEqual({
179+
generation: 2,
180+
identity: "[email protected]",
181+
});
177182
expect(museUsageAccount(apiKey)).toBeUndefined();
178183
const other = nextMuseUsageAccount(named, { ...login, email: "[email protected]" });
179184
expect(other).toEqual({ generation: 1, identity: "[email protected]" });

‎packages/provider-muse/src/server/usageLimits.ts‎

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@ export function museUsageLimitResetAt(
5858

5959
const WEEK_MINS = 7 * 24 * 60;
6060

61-
/** One report of the account's windows, as Muse forwards it. */
62-
export interface MuseUsageObservation {
63-
readonly observedAtMs: number;
64-
/** Meta's five-hour-class block. */
65-
readonly window?: {
66-
readonly usedPercent: number;
67-
readonly windowDurationMins: number;
68-
readonly resetsAtMs: number;
69-
};
70-
readonly weekly?: { readonly usedPercent: number; readonly resetsAtMs: number };
71-
}
61+
type MuseUsageObservation = Pick<MuseSubscriptionUsage, "observedAtMs" | "window" | "weekly">;
7262

7363
const isoFromMillis = (ms: number) => DateTime.formatIso(DateTime.makeUnsafe(ms));
7464

@@ -99,20 +89,8 @@ export function museUsageWindows(
9989
): ReadonlyArray<ServerProviderUsageWindow> {
10090
const { window, weekly } = observation;
10191
return [
102-
...(window
103-
? [
104-
makeWindow(
105-
"window",
106-
window.windowDurationMins,
107-
window.usedPercent,
108-
window.resetsAtMs,
109-
nowMs,
110-
),
111-
]
112-
: []),
113-
...(weekly
114-
? [makeWindow("weekly", WEEK_MINS, weekly.usedPercent, weekly.resetsAtMs, nowMs)]
115-
: []),
92+
makeWindow("window", window.windowDurationMins, window.usedPercent, window.resetsAtMs, nowMs),
93+
makeWindow("weekly", WEEK_MINS, weekly.usedPercent, weekly.resetsAtMs, nowMs),
11694
];
11795
}
11896

@@ -148,7 +126,8 @@ export function nextMuseUsageAccount(
148126
account: MuseUsageAccount,
149127
auth: ServerProviderAuth,
150128
): MuseUsageAccount {
151-
const identity = auth.status === "unauthenticated" ? null : museUsageAccount(auth);
129+
const identity =
130+
auth.status === "unauthenticated" || auth.type === "apiKey" ? null : museUsageAccount(auth);
152131
if (identity === undefined || identity === account.identity) return account;
153132
// The first login a check names is the one the hosts so far started under.
154133
if (account.identity === undefined) return { ...account, identity };

0 commit comments

Comments
 (0)