Skip to content

Commit ae4fee8

Browse files
committed
fix(server): use proxy quotas for Claude limit recovery
1 parent 6d82a22 commit ae4fee8

6 files changed

Lines changed: 307 additions & 6 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
TaskUpdateInput,
2424
TaskUpdateOutput,
2525
} from "@anthropic-ai/claude-agent-sdk/sdk-tools";
26+
import * as UsageLimitSources from "../../usage/UsageLimitSources.ts";
2627
import * as NodeServices from "@effect/platform-node/NodeServices";
2728
import {
2829
ChatAttachmentId,
@@ -5750,6 +5751,69 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57505751
),
57515752
);
57525753

5754+
it.effect.each(["environment", "settings"])(
5755+
"uses the matching proxy reset from %s when Claude omits a reset time",
5756+
(source) =>
5757+
Effect.gen(function* () {
5758+
const fs = yield* FileSystem.FileSystem;
5759+
const path = yield* Path.Path;
5760+
const configDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-proxy-reset-" });
5761+
if (source === "settings")
5762+
yield* fs.writeFileString(
5763+
path.join(configDir, "settings.json"),
5764+
JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://hub.test/v1" } }),
5765+
);
5766+
const harness = yield* makeWakeHarnessWithOptions({
5767+
environment: {
5768+
CLAUDE_CONFIG_DIR: configDir,
5769+
...(source === "environment" ? { ANTHROPIC_BASE_URL: "https://hub.test/v1" } : {}),
5770+
},
5771+
});
5772+
const now = yield* DateTime.now;
5773+
yield* harness.runtime.startTurn(
5774+
makeClaudeTestTurnInput({
5775+
threadId: harness.threadId,
5776+
providerThread: harness.providerThread,
5777+
now,
5778+
attemptId: RunAttemptId.make("attempt-proxy-reset"),
5779+
text: "Continue.",
5780+
attachments: [],
5781+
}),
5782+
);
5783+
yield* Queue.offerAll(harness.sdkMessages, [
5784+
makeAssistantErrorFrame({
5785+
uuid: "00000000-0000-4000-8000-000000000652",
5786+
error: "rate_limit",
5787+
}),
5788+
makeResultFrame({
5789+
uuid: "00000000-0000-4000-8000-000000000653",
5790+
result: "Usage limit reached.",
5791+
terminalReason: "blocking_limit",
5792+
isError: true,
5793+
apiErrorStatus: 429,
5794+
}),
5795+
]);
5796+
const terminal = yield* Queue.take(harness.terminalReceipts);
5797+
assert.equal(terminal.status, "failed");
5798+
if (terminal.status !== "failed") return;
5799+
assert.equal(terminal.failure.class, "usage_limit");
5800+
assert.equal(terminal.failure.resetAt, "2099-01-01T02:00:00.000Z");
5801+
}).pipe(
5802+
Effect.provide([
5803+
IdAllocator.layer,
5804+
McpProviderSessions.layer,
5805+
NodeServices.layer,
5806+
Layer.mock(UsageLimitSources.UsageLimitSources)({
5807+
resolveResetAt: (input) => {
5808+
assert.equal(input.endpoint, "https://hub.test/v1");
5809+
assert.isNotEmpty(input.model);
5810+
return Effect.succeed("2099-01-01T02:00:00.000Z");
5811+
},
5812+
}),
5813+
]),
5814+
),
5815+
);
5816+
57535817
it.effect.each([
57545818
{ status: 503, cooldown: "4m 6s", seconds: 246 },
57555819
{ status: 503, cooldown: "1h 2m 3s", seconds: 3723 },

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ import {
140140
compileClaudeModelSelection,
141141
type CompiledClaudeModelSelection,
142142
} from "../../claudeModelOptions.ts";
143+
import * as UsageLimitSources from "../../usage/UsageLimitSources.ts";
143144
import * as ServerConfig from "../../config.ts";
144145
import * as PortScanner from "../../preview/PortScanner.ts";
145146
import * as ServerSettings from "../../serverSettings.ts";
@@ -2596,6 +2597,14 @@ const ClaudeBypassSettings = Schema.StructWithRest(
25962597
}),
25972598
[Schema.Record(Schema.String, Schema.Unknown)],
25982599
);
2600+
const decodeClaudeEndpointSettings = Schema.decodeUnknownEffect(
2601+
Schema.fromJsonString(
2602+
Schema.Struct({
2603+
env: Schema.optional(Schema.Struct({ ANTHROPIC_BASE_URL: Schema.optional(Schema.String) })),
2604+
}),
2605+
),
2606+
);
2607+
25992608
const decodeClaudeBypassSettings = Schema.decodeUnknownEffect(
26002609
fromLenientJson(ClaudeBypassSettings),
26012610
);
@@ -4367,7 +4376,38 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
43674376
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
43684377
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
43694378
const serverSettings = yield* Effect.serviceOption(ServerSettings.ServerSettingsService);
4379+
const usageLimitSources = yield* Effect.serviceOption(UsageLimitSources.UsageLimitSources);
43704380
const { attachmentsDir, fileSystem, path, crypto, idAllocator, queryRunner } = adapterOptions;
4381+
const proxyResetAt = Effect.fn("ClaudeAdapterV2.proxyResetAt")(function* (
4382+
context: ActiveClaudeTurnContext,
4383+
cwd: string,
4384+
) {
4385+
if (Option.isNone(usageLimitSources)) return null;
4386+
4387+
let endpoint = adapterOptions.environment.ANTHROPIC_BASE_URL;
4388+
const settingsPaths = [
4389+
path.join(homePath, "settings.json"),
4390+
...(cwd
4391+
? [
4392+
path.join(cwd, ".claude", "settings.json"),
4393+
path.join(cwd, ".claude", "settings.local.json"),
4394+
]
4395+
: []),
4396+
];
4397+
for (const settingsPath of settingsPaths) {
4398+
const settings = yield* fileSystem.readFileString(settingsPath).pipe(
4399+
Effect.flatMap(decodeClaudeEndpointSettings),
4400+
Effect.orElseSucceed(() => undefined),
4401+
);
4402+
endpoint = settings?.env?.ANTHROPIC_BASE_URL ?? endpoint;
4403+
}
4404+
if (!endpoint) return null;
4405+
return yield* usageLimitSources.value.resolveResetAt({
4406+
endpoint,
4407+
model: compileClaudeModelSelection(context.input.modelSelection).apiModelId,
4408+
});
4409+
});
4410+
43714411
const subagentModelCatalog = scopeClaudeModelCatalog(
43724412
BUNDLED_CLAUDE_MODEL_CATALOG,
43734413
adapterOptions.settings.customModels,
@@ -9217,9 +9257,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92179257
code: "compaction_failed",
92189258
})
92199259
: providerFailureFromResult(message, completedAt, failureHint, usageLimited);
9260+
const fallbackResetAt =
9261+
resultFailure?.class === "usage_limit" &&
9262+
resetAt === null &&
9263+
resultFailure.resetAt == null
9264+
? yield* proxyResetAt(context, session.cwd)
9265+
: null;
92209266
const terminalFailure =
92219267
resultFailure?.class === "usage_limit"
9222-
? { ...resultFailure, resetAt: resetAt ?? resultFailure.resetAt ?? null }
9268+
? { ...resultFailure, resetAt: resetAt ?? resultFailure.resetAt ?? fallbackResetAt }
92239269
: resultFailure;
92249270
yield* finalizeActiveTurn({
92259271
context,

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,10 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll(
634634
Layer.provideMerge(layerVcs),
635635
Layer.provideMerge(Layer.mergeAll(layerTerminal, layerPreview, layerDevice)),
636636
Layer.provideMerge(layerPersistence),
637-
Layer.provideMerge(
638-
Layer.mergeAll(Keybindings.layer, EnvironmentTheme.layer, UsageLimitSources.layer),
639-
),
637+
Layer.provideMerge(Layer.mergeAll(Keybindings.layer, EnvironmentTheme.layer)),
640638
Layer.provideMerge(ProviderRegistry.layer),
641639
Layer.provideMerge(ProviderInstanceRegistryHydration.layer),
640+
Layer.provideMerge(UsageLimitSources.layer),
642641
// Provider hydration builds the Claude runner, which reports agents to the same scanner the
643642
// terminal and preview layers share.
644643
Layer.provideMerge(layerPortScanner),

‎apps/server/src/usage/UsageLimitSources.test.ts‎

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"
44
import * as Effect from "effect/Effect";
55
import * as Layer from "effect/Layer";
66
import * as Schema from "effect/Schema";
7-
import { HttpClient } from "effect/http";
7+
import { HttpClient, HttpClientResponse } from "effect/http";
88

99
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
1010
import * as ServerSettings from "../serverSettings.ts";
@@ -83,3 +83,127 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
8383
Effect.scoped,
8484
),
8585
);
86+
87+
it.effect.each([
88+
{ scenario: "multiple windows", expected: "2099-01-01T02:00:00.000Z" },
89+
{ scenario: "model scopes", expected: "2099-01-01T04:00:00.000Z" },
90+
{ scenario: "disabled account", expected: "2099-01-01T03:00:00.000Z" },
91+
{ scenario: "model list unavailable", expected: "2099-01-01T03:00:00.000Z" },
92+
{ scenario: "missing resets", expected: null },
93+
{ scenario: "hub unavailable", expected: null },
94+
{ scenario: "disabled source", expected: null },
95+
])("resolves only the matching hub and model: $scenario", ({ scenario, expected }) =>
96+
Effect.gen(function* () {
97+
const sources = yield* UsageLimitSources.make;
98+
const unrelated = yield* sources.resolveResetAt({
99+
endpoint: "https://other.test",
100+
model: "claude-opus-4-6",
101+
});
102+
expect(unrelated).toBeNull();
103+
const reset = yield* sources.resolveResetAt({
104+
endpoint: "https://hub.test/v1",
105+
model: "claude-opus-4-6",
106+
});
107+
expect(reset).toBe(expected);
108+
const unlisted = yield* sources.resolveResetAt({
109+
endpoint: "https://hub.test/v1",
110+
model: "unlisted",
111+
});
112+
expect(unlisted).toBeNull();
113+
}).pipe(
114+
Effect.provide([
115+
ServerSettings.layerTest({
116+
usageLimitSources: {
117+
[UsageLimitSourceId.make("hub")]: {
118+
kind: "cliproxy",
119+
url: "https://hub.test",
120+
managementKey: "test",
121+
enabled: scenario !== "disabled source",
122+
},
123+
},
124+
}),
125+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
126+
shouldRunScopeWork: () => Effect.succeed(false),
127+
}),
128+
Layer.succeed(
129+
HttpClient.HttpClient,
130+
HttpClient.make((request) =>
131+
Effect.sync(() => {
132+
const url = new URL(request.url);
133+
if (
134+
scenario === "hub unavailable" ||
135+
(scenario === "model list unavailable" && url.searchParams.get("name") === "fast")
136+
) {
137+
return HttpClientResponse.fromWeb(request, new Response(null, { status: 503 }));
138+
}
139+
const files = ["slow", "fast", "wrong-model"];
140+
const body = url.pathname.endsWith("/auth-files")
141+
? {
142+
files: files.map((id) => ({
143+
id,
144+
auth_index: id,
145+
provider: "claude",
146+
disabled: scenario === "disabled account" && id === "fast",
147+
})),
148+
}
149+
: url.pathname.endsWith("/auth-files/models")
150+
? {
151+
models: [
152+
{
153+
id:
154+
url.searchParams.get("name") === "wrong-model"
155+
? "claude-sonnet-4-6"
156+
: "claude-opus-4-6",
157+
},
158+
],
159+
}
160+
: (() => {
161+
const input = JSON.parse(
162+
request.body._tag === "Uint8Array"
163+
? new TextDecoder().decode(request.body.body)
164+
: "{}",
165+
);
166+
const hour =
167+
input.auth_index === "slow"
168+
? "03"
169+
: input.auth_index === "fast"
170+
? "02"
171+
: "01";
172+
return {
173+
status_code: 200,
174+
body: JSON.stringify({
175+
five_hour: {
176+
utilization: 100,
177+
resets_at: scenario === "missing resets" ? null : "2099-01-01T01:00:00Z",
178+
},
179+
seven_day: { utilization: 100, resets_at: `2099-01-01T${hour}:00:00Z` },
180+
...(scenario === "model scopes"
181+
? {
182+
limits: [
183+
{
184+
kind: "weekly_scoped",
185+
percent: 100,
186+
resets_at: "2099-01-01T04:00:00Z",
187+
scope: { model: { display_name: "Opus 4.6" } },
188+
},
189+
{
190+
kind: "weekly_scoped",
191+
percent: 100,
192+
resets_at: "2099-01-01T05:00:00Z",
193+
scope: { model: { display_name: "Sonnet" } },
194+
},
195+
],
196+
}
197+
: {}),
198+
}),
199+
};
200+
})();
201+
return HttpClientResponse.fromWeb(request, Response.json(body));
202+
}),
203+
),
204+
),
205+
NodeCrypto.layer,
206+
]),
207+
Effect.scoped,
208+
),
209+
);

‎apps/server/src/usage/UsageLimitSources.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class UsageLimitSources extends Context.Service<
3030
readonly current: Effect.Effect<ReadonlyArray<UsageLimitSourceSnapshot>>;
3131
readonly streamChanges: Stream.Stream<ReadonlyArray<UsageLimitSourceSnapshot>>;
3232
readonly refresh: Effect.Effect<void>;
33+
readonly resolveResetAt: (input: {
34+
readonly endpoint: string;
35+
readonly model: string;
36+
}) => Effect.Effect<string | null>;
3337
readonly consumeResetCredit: (
3438
input: UsageLimitSourceConsumeResetCreditInput,
3539
) => Effect.Effect<ProviderConsumeResetCreditResult, UsageLimitSourceError>;
@@ -149,8 +153,57 @@ export const make = Effect.gen(function* () {
149153

150154
yield* refresh.pipe(Effect.forkScoped);
151155

156+
const resolveResetAt = Effect.fn("UsageLimitSources.resolveResetAt")(
157+
function* (input: { readonly endpoint: string; readonly model: string }) {
158+
const endpoint = URL.parse(input.endpoint);
159+
if (!endpoint || !["http:", "https:"].includes(endpoint.protocol)) return null;
160+
const settings = yield* settingsService.getSettings;
161+
const entries = Object.entries(settings.usageLimitSources).filter(([, config]) => {
162+
const hub = URL.parse(config.url);
163+
if (!config.enabled || !config.managementKey || !hub || hub.origin !== endpoint.origin)
164+
return false;
165+
const basePath = hub.pathname.replace(/\/$/, "");
166+
return endpoint.pathname === basePath || endpoint.pathname.startsWith(`${basePath}/`);
167+
});
168+
if (entries.length !== 1) return null;
169+
const [id, config] = entries[0]!;
170+
const snapshot = yield* readSource(id as UsageLimitSourceId, config);
171+
const now = DateTime.toEpochMillis(yield* DateTime.now);
172+
const resets = yield* Effect.forEach(
173+
snapshot.accounts.filter((account) => account.driver === "claudeAgent"),
174+
(account) =>
175+
Effect.gen(function* () {
176+
const models = yield* api.accountModels(config, account.id);
177+
if (!models.includes(input.model) || account.usageLimits.unavailable) return null;
178+
const model = input.model.replace(/^claude-/, "").replace(/[^a-z0-9]+/g, "_");
179+
const blocked = account.usageLimits.windows.filter((window) => {
180+
if (window.usedPercent < 100) return false;
181+
if (window.id === "five_hour" || window.id === "seven_day") return true;
182+
const scope = window.id.replace(/^seven_day_/, "").replace(/^claude_/, "");
183+
if (!/^(opus|sonnet|haiku)(_|$)/.test(scope)) return true;
184+
return model === scope || model.startsWith(`${scope}_`);
185+
});
186+
if (
187+
blocked.length === 0 ||
188+
blocked.some((window) => !window.resetsAt || Date.parse(window.resetsAt) <= now)
189+
)
190+
return null;
191+
return Math.max(...blocked.map((window) => Date.parse(window.resetsAt!)));
192+
}).pipe(Effect.orElseSucceed(() => null)),
193+
{ concurrency: 4 },
194+
);
195+
const known = resets.filter((reset) => reset !== null);
196+
return known.length === 0
197+
? null
198+
: DateTime.formatIso(DateTime.makeUnsafe(Math.min(...known)));
199+
},
200+
Effect.timeout("15 seconds"),
201+
Effect.orElseSucceed(() => null),
202+
);
203+
152204
return {
153205
current: Ref.get(stateRef),
206+
resolveResetAt,
154207
consumeResetCredit,
155208
refresh,
156209
get streamChanges() {

0 commit comments

Comments
 (0)