Skip to content

Commit cb6bcb2

Browse files
committed
fix(server): honor Claude settings precedence for proxy recovery
1 parent ae4fee8 commit cb6bcb2

2 files changed

Lines changed: 112 additions & 64 deletions

File tree

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

Lines changed: 98 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5751,67 +5751,105 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57515751
),
57525752
);
57535753

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-
}),
5754+
it.effect.each([
5755+
"environment",
5756+
"settings",
5757+
"lenient settings",
5758+
"root local",
5759+
"launch inline",
5760+
"launch file",
5761+
])("uses the matching proxy reset from %s when Claude omits a reset time", (source) =>
5762+
Effect.gen(function* () {
5763+
const fs = yield* FileSystem.FileSystem;
5764+
const path = yield* Path.Path;
5765+
const configDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-proxy-reset-" });
5766+
if (source === "settings")
5767+
yield* fs.writeFileString(
5768+
path.join(configDir, "settings.json"),
5769+
JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://hub.test/v1" } }),
5770+
);
5771+
const cwd = path.join(configDir, "repo", "nested");
5772+
yield* fs.makeDirectory(path.join(configDir, "repo", ".git"), { recursive: true });
5773+
yield* fs.makeDirectory(path.join(cwd, ".claude"), { recursive: true });
5774+
if (source === "lenient settings")
5775+
yield* fs.writeFileString(
5776+
path.join(configDir, "settings.json"),
5777+
'{ // Claude settings allow comments and trailing commas\n "env": { "ANTHROPIC_BASE_URL": "https://hub.test/v1", }, }',
5778+
);
5779+
if (source === "root local") {
5780+
yield* fs.writeFileString(
5781+
path.join(cwd, ".claude", "settings.local.json"),
5782+
JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://wrong.test" } }),
5783+
);
5784+
yield* fs.makeDirectory(path.join(configDir, "repo", ".claude"));
5785+
yield* fs.writeFileString(
5786+
path.join(configDir, "repo", ".claude", "settings.local.json"),
5787+
JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://hub.test/v1" } }),
57825788
);
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-
),
5789+
}
5790+
const launchSettings = JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://hub.test/v1" } });
5791+
if (source === "launch file")
5792+
yield* fs.writeFileString(path.join(cwd, "launch.json"), launchSettings);
5793+
const harness = yield* makeWakeHarnessWithOptions({
5794+
settings: {
5795+
...DEFAULT_CLAUDE_SETTINGS,
5796+
launchArgs:
5797+
source === "launch inline"
5798+
? `--settings '${launchSettings}'`
5799+
: source === "launch file"
5800+
? "--settings launch.json"
5801+
: "",
5802+
},
5803+
environment: {
5804+
CLAUDE_CONFIG_DIR: configDir,
5805+
ANTHROPIC_BASE_URL:
5806+
source === "environment" ? "https://hub.test/v1" : "https://wrong.test",
5807+
},
5808+
});
5809+
const now = yield* DateTime.now;
5810+
yield* harness.runtime.startTurn(
5811+
makeClaudeTestTurnInput({
5812+
threadId: harness.threadId,
5813+
providerThread: harness.providerThread,
5814+
now,
5815+
attemptId: RunAttemptId.make("attempt-proxy-reset"),
5816+
text: "Continue.",
5817+
attachments: [],
5818+
runtimePolicy: { ...CLAUDE_TEST_RUNTIME_POLICY, cwd },
5819+
}),
5820+
);
5821+
yield* Queue.offerAll(harness.sdkMessages, [
5822+
makeAssistantErrorFrame({
5823+
uuid: "00000000-0000-4000-8000-000000000652",
5824+
error: "rate_limit",
5825+
}),
5826+
makeResultFrame({
5827+
uuid: "00000000-0000-4000-8000-000000000653",
5828+
result: "Usage limit reached.",
5829+
terminalReason: "blocking_limit",
5830+
isError: true,
5831+
apiErrorStatus: 429,
5832+
}),
5833+
]);
5834+
const terminal = yield* Queue.take(harness.terminalReceipts);
5835+
assert.equal(terminal.status, "failed");
5836+
if (terminal.status !== "failed") return;
5837+
assert.equal(terminal.failure.class, "usage_limit");
5838+
assert.equal(terminal.failure.resetAt, "2099-01-01T02:00:00.000Z");
5839+
}).pipe(
5840+
Effect.provide([
5841+
IdAllocator.layer,
5842+
McpProviderSessions.layer,
5843+
NodeServices.layer,
5844+
Layer.mock(UsageLimitSources.UsageLimitSources)({
5845+
resolveResetAt: (input) => {
5846+
assert.equal(input.endpoint, "https://hub.test/v1");
5847+
assert.isNotEmpty(input.model);
5848+
return Effect.succeed("2099-01-01T02:00:00.000Z");
5849+
},
5850+
}),
5851+
]),
5852+
),
58155853
);
58165854

58175855
it.effect.each([

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,7 +2598,7 @@ const ClaudeBypassSettings = Schema.StructWithRest(
25982598
[Schema.Record(Schema.String, Schema.Unknown)],
25992599
);
26002600
const decodeClaudeEndpointSettings = Schema.decodeUnknownEffect(
2601-
Schema.fromJsonString(
2601+
fromLenientJson(
26022602
Schema.Struct({
26032603
env: Schema.optional(Schema.Struct({ ANTHROPIC_BASE_URL: Schema.optional(Schema.String) })),
26042604
}),
@@ -4380,17 +4380,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
43804380
const { attachmentsDir, fileSystem, path, crypto, idAllocator, queryRunner } = adapterOptions;
43814381
const proxyResetAt = Effect.fn("ClaudeAdapterV2.proxyResetAt")(function* (
43824382
context: ActiveClaudeTurnContext,
4383-
cwd: string,
43844383
) {
43854384
if (Option.isNone(usageLimitSources)) return null;
4386-
4385+
const cwd = context.input.runtimePolicy.cwd;
4386+
const localDirectory = cwd === null ? null : yield* localSettingsDirectory(cwd);
43874387
let endpoint = adapterOptions.environment.ANTHROPIC_BASE_URL;
43884388
const settingsPaths = [
43894389
path.join(homePath, "settings.json"),
43904390
...(cwd
43914391
? [
43924392
path.join(cwd, ".claude", "settings.json"),
43934393
path.join(cwd, ".claude", "settings.local.json"),
4394+
...(localDirectory === null || localDirectory === cwd
4395+
? []
4396+
: [path.join(localDirectory, ".claude", "settings.local.json")]),
43944397
]
43954398
: []),
43964399
];
@@ -4401,6 +4404,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
44014404
);
44024405
endpoint = settings?.env?.ANTHROPIC_BASE_URL ?? endpoint;
44034406
}
4407+
const launchSettings = yield* readClaudeLaunchArgSettings({
4408+
settings: adapterOptions.settings,
4409+
cwd,
4410+
fileSystem,
4411+
path,
4412+
}).pipe(Effect.orElseSucceed(() => undefined));
4413+
endpoint = launchSettings?.env?.ANTHROPIC_BASE_URL ?? endpoint;
44044414
if (!endpoint) return null;
44054415
return yield* usageLimitSources.value.resolveResetAt({
44064416
endpoint,
@@ -9261,7 +9271,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92619271
resultFailure?.class === "usage_limit" &&
92629272
resetAt === null &&
92639273
resultFailure.resetAt == null
9264-
? yield* proxyResetAt(context, session.cwd)
9274+
? yield* proxyResetAt(context)
92659275
: null;
92669276
const terminalFailure =
92679277
resultFailure?.class === "usage_limit"

0 commit comments

Comments
 (0)