Skip to content

Commit 17d7993

Browse files
committed
fix(server): use live Claude query for quota recovery
1 parent 986a775 commit 17d7993

2 files changed

Lines changed: 70 additions & 45 deletions

File tree

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

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5759,7 +5759,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57595759
"process cwd",
57605760
"launch inline",
57615761
"launch file",
5762-
"stalled settings",
5762+
"stalled lookup",
5763+
"reused endpoint",
5764+
"reused model",
57635765
])("uses the matching proxy reset from %s when Claude omits a reset time", (source) =>
57645766
Effect.gen(function* () {
57655767
const fs = yield* FileSystem.FileSystem;
@@ -5797,15 +5799,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57975799
const launchSettings = JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://hub.test/v1" } });
57985800
if (source === "launch file")
57995801
yield* fs.writeFileString(path.join(cwd, "launch.json"), launchSettings);
5800-
const readStarted = yield* Deferred.make<void>();
5801-
let stallSettingsRead = false;
5802-
const stalledFileSystem = {
5803-
...fs,
5804-
readFileString: (filePath: string) =>
5805-
stallSettingsRead && filePath === path.join(configDir, "settings.json")
5806-
? Deferred.succeed(readStarted, undefined).pipe(Effect.andThen(Effect.never))
5807-
: fs.readFileString(filePath),
5808-
};
5802+
const lookupStarted = yield* Deferred.make<void>();
58095803
const harness = yield* makeWakeHarnessWithOptions({
58105804
settings: {
58115805
...DEFAULT_CLAUDE_SETTINGS,
@@ -5819,11 +5813,25 @@ describe("ClaudeAdapterV2 background wake turns", () => {
58195813
environment: {
58205814
CLAUDE_CONFIG_DIR: configDir,
58215815
ANTHROPIC_BASE_URL:
5822-
source === "environment" ? "https://hub.test/v1" : "https://wrong.test",
5816+
source === "environment" || source.startsWith("reused") || source === "stalled lookup"
5817+
? "https://hub.test/v1"
5818+
: "https://wrong.test",
58235819
},
58245820
}).pipe(
58255821
Effect.provideService(HostProcess.WorkingDirectory, cwd),
5826-
Effect.provideService(FileSystem.FileSystem, stalledFileSystem),
5822+
Effect.provide(
5823+
Layer.mock(UsageLimitSources.UsageLimitSources)({
5824+
resolveResetAt: (input) => {
5825+
assert.equal(input.endpoint, "https://hub.test/v1");
5826+
assert.equal(input.model, CLAUDE_TEST_MODEL_SELECTION.model);
5827+
if (source === "stalled lookup")
5828+
return Deferred.succeed(lookupStarted, undefined).pipe(
5829+
Effect.andThen(Effect.never),
5830+
);
5831+
return Effect.succeed("2099-01-01T02:00:00.000Z");
5832+
},
5833+
}),
5834+
),
58275835
);
58285836
const now = yield* DateTime.now;
58295837
yield* harness.runtime.startTurn(
@@ -5836,11 +5844,34 @@ describe("ClaudeAdapterV2 background wake turns", () => {
58365844
attachments: [],
58375845
runtimePolicy: {
58385846
...CLAUDE_TEST_RUNTIME_POLICY,
5839-
cwd: source === "process cwd" || source === "stalled settings" ? null : cwd,
5847+
cwd: source === "process cwd" || source === "stalled lookup" ? null : cwd,
58405848
},
58415849
}),
58425850
);
5843-
stallSettingsRead = source === "stalled settings";
5851+
if (source.startsWith("reused")) {
5852+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
5853+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
5854+
if (source === "reused endpoint")
5855+
yield* fs.writeFileString(
5856+
path.join(configDir, "settings.json"),
5857+
JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://wrong.test" } }),
5858+
);
5859+
yield* harness.runtime.startTurn(
5860+
makeClaudeTestTurnInput({
5861+
threadId: harness.threadId,
5862+
providerThread: harness.providerThread,
5863+
now,
5864+
attemptId: RunAttemptId.make("attempt-proxy-continuation"),
5865+
providerTurnOrdinal: 2,
5866+
messageCreatedBy: "agent",
5867+
messageCreationSource: "provider",
5868+
modelSelection: CLAUDE_TEST_RESTART_MODEL_SELECTION,
5869+
runtimePolicy: { ...CLAUDE_TEST_RUNTIME_POLICY, cwd: configDir },
5870+
text: "Background task completed.",
5871+
attachments: [],
5872+
}),
5873+
);
5874+
}
58445875
yield* Queue.offerAll(harness.sdkMessages, [
58455876
makeAssistantErrorFrame({
58465877
uuid: "00000000-0000-4000-8000-000000000652",
@@ -5854,8 +5885,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
58545885
apiErrorStatus: 429,
58555886
}),
58565887
]);
5857-
if (source === "stalled settings") {
5858-
yield* Deferred.await(readStarted);
5888+
if (source === "stalled lookup") {
5889+
yield* Deferred.await(lookupStarted);
58595890
yield* TestClock.adjust("15 seconds");
58605891
}
58615892
const terminal = yield* Queue.take(harness.terminalReceipts);
@@ -5864,22 +5895,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
58645895
assert.equal(terminal.failure.class, "usage_limit");
58655896
assert.equal(
58665897
terminal.failure.resetAt,
5867-
source === "stalled settings" ? null : "2099-01-01T02:00:00.000Z",
5898+
source === "stalled lookup" ? null : "2099-01-01T02:00:00.000Z",
58685899
);
5869-
}).pipe(
5870-
Effect.provide([
5871-
IdAllocator.layer,
5872-
McpProviderSessions.layer,
5873-
NodeServices.layer,
5874-
Layer.mock(UsageLimitSources.UsageLimitSources)({
5875-
resolveResetAt: (input) => {
5876-
assert.equal(input.endpoint, "https://hub.test/v1");
5877-
assert.isNotEmpty(input.model);
5878-
return Effect.succeed("2099-01-01T02:00:00.000Z");
5879-
},
5880-
}),
5881-
]),
5882-
),
5900+
}).pipe(Effect.provide([IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer])),
58835901
);
58845902

58855903
it.effect.each([

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3964,6 +3964,7 @@ interface ClaudeLiveQueryContext {
39643964
readonly nativeThreadId: string;
39653965
readonly query: ClaudeAgentSdkQuerySession;
39663966
readonly queryPolicyKey: string;
3967+
readonly proxyEndpoint: string | null;
39673968
selection: CompiledClaudeModelSelection;
39683969
readonly closed: Deferred.Deferred<void, never>;
39693970
lastTurn: ActiveClaudeTurnContext | null;
@@ -4379,10 +4380,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
43794380
const usageLimitSources = yield* Effect.serviceOption(UsageLimitSources.UsageLimitSources);
43804381
const workingDirectory = yield* HostProcess.WorkingDirectory;
43814382
const { attachmentsDir, fileSystem, path, crypto, idAllocator, queryRunner } = adapterOptions;
4382-
const proxyResetAt = Effect.fn("ClaudeAdapterV2.proxyResetAt")(
4383-
function* (context: ActiveClaudeTurnContext) {
4383+
const resolveProxyEndpoint = Effect.fn("ClaudeAdapterV2.resolveProxyEndpoint")(
4384+
function* (queryCwd: string | null, launchSettings: ClaudeSdkSettings | undefined) {
43844385
if (Option.isNone(usageLimitSources)) return null;
4385-
const cwd = context.input.runtimePolicy.cwd ?? workingDirectory;
4386+
const cwd = queryCwd ?? workingDirectory;
43864387
const localDirectory = yield* localSettingsDirectory(cwd);
43874388
let endpoint = adapterOptions.environment.ANTHROPIC_BASE_URL;
43884389
const settingsPaths = [
@@ -4400,17 +4401,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
44004401
);
44014402
endpoint = settings?.env?.ANTHROPIC_BASE_URL ?? endpoint;
44024403
}
4403-
const launchSettings = yield* readClaudeLaunchArgSettings({
4404-
settings: adapterOptions.settings,
4405-
cwd,
4406-
fileSystem,
4407-
path,
4408-
}).pipe(Effect.orElseSucceed(() => undefined));
4409-
endpoint = launchSettings?.env?.ANTHROPIC_BASE_URL ?? endpoint;
4410-
if (!endpoint) return null;
4404+
return launchSettings?.env?.ANTHROPIC_BASE_URL ?? endpoint ?? null;
4405+
},
4406+
Effect.timeout("15 seconds"),
4407+
Effect.orElseSucceed(() => null),
4408+
);
4409+
4410+
const proxyResetAt = Effect.fn("ClaudeAdapterV2.proxyResetAt")(
4411+
function* (liveQuery: ClaudeLiveQueryContext) {
4412+
if (Option.isNone(usageLimitSources) || !liveQuery.proxyEndpoint) return null;
44114413
return yield* usageLimitSources.value.resolveResetAt({
4412-
endpoint,
4413-
model: compileClaudeModelSelection(context.input.modelSelection).apiModelId,
4414+
endpoint: liveQuery.proxyEndpoint,
4415+
model: liveQuery.selection.apiModelId,
44144416
});
44154417
},
44164418
Effect.timeout("15 seconds"),
@@ -9270,7 +9272,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92709272
resultFailure?.class === "usage_limit" &&
92719273
resetAt === null &&
92729274
resultFailure.resetAt == null
9273-
? yield* proxyResetAt(context)
9275+
? yield* proxyResetAt(liveQuery)
92749276
: null;
92759277
const terminalFailure =
92769278
resultFailure?.class === "usage_limit"
@@ -10000,6 +10002,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1000010002
...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }),
1000110003
...(toolSearchOverride === undefined ? {} : { toolSearchOverride }),
1000210004
});
10005+
const proxyEndpoint = yield* resolveProxyEndpoint(
10006+
turnInput.runtimePolicy.cwd,
10007+
launchArgSettings,
10008+
);
1000310009
const querySession = yield* queryRunner
1000410010
.open({
1000510011
threadId: turnInput.threadId,
@@ -10037,6 +10043,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1003710043
query: querySession,
1003810044
queryPolicyKey,
1003910045
selection: compiledSelection,
10046+
proxyEndpoint,
1004010047
closed,
1004110048
lastTurn: null,
1004210049
promptEchoMode: "unknown",

0 commit comments

Comments
 (0)