Skip to content

Commit c6bb998

Browse files
committed
fix(server): a full Claude context window no longer shows as a usage limit
Claude ends a turn with terminal_reason "blocking_limit" both when a plan limit blocks the request and when the context window is full ("Prompt is too long"). The adapter treated every blocking_limit as a usage limit, so a full context showed "Usage limit reached" with no reset time, and retrying could never work. blocking_limit is now a usage limit only with rate-limit evidence (a rejected rate_limit_event or a rate_limit assistant error), as already decided for other terminal reasons. Without it the turn fails as a provider error that says the context window is full and points to /compact or a new thread. Refs #19832
1 parent 339179f commit c6bb998

2 files changed

Lines changed: 81 additions & 11 deletions

File tree

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

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22112211
});
22122212
const makeAssistantErrorFrame = (input: {
22132213
readonly uuid: string;
2214-
readonly error: "authentication_failed" | "rate_limit" | "server_error" | undefined;
2214+
readonly error:
2215+
| "authentication_failed"
2216+
| "invalid_request"
2217+
| "rate_limit"
2218+
| "server_error"
2219+
| undefined;
2220+
readonly text?: string;
22152221
readonly parentToolUseId?: string | null;
22162222
}) =>
22172223
claudeSdkFrame({
@@ -2221,7 +2227,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22212227
id: `msg_${input.uuid}`,
22222228
type: "message",
22232229
role: "assistant",
2224-
content: [{ type: "text", text: "Claude could not complete this request." }],
2230+
content: [{ type: "text", text: input.text ?? "Claude could not complete this request." }],
22252231
stop_reason: null,
22262232
stop_sequence: null,
22272233
usage: {
@@ -4639,10 +4645,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
46394645
assert.equal(terminal.status, "failed");
46404646
if (terminal.status !== "failed") return;
46414647
assert.isNotEmpty(terminal.failure.message);
4642-
assert.equal(
4643-
terminal.failure.class,
4644-
terminalReason === "blocking_limit" ? "usage_limit" : "provider_error",
4645-
);
4648+
assert.equal(terminal.failure.class, "provider_error");
46464649
assert.isFalse(
46474650
harness.events.some(
46484651
(event) =>
@@ -4662,6 +4665,70 @@ describe("ClaudeAdapterV2 background wake turns", () => {
46624665
),
46634666
);
46644667

4668+
// Frames follow #19832: a full context window ends the turn with
4669+
// blocking_limit while the plan's rate-limit window is still allowed.
4670+
it.effect.each([
4671+
{
4672+
windowStatus: "allowed",
4673+
expectedClass: "provider_error",
4674+
expectedMessage: "Claude stopped: the prompt exceeds the model's context window.",
4675+
},
4676+
{
4677+
windowStatus: "rejected",
4678+
expectedClass: "usage_limit",
4679+
expectedMessage: "Claude usage limit reached. Send the message again once the limit resets.",
4680+
},
4681+
] as const)(
4682+
"classifies blocking_limit by its rate-limit evidence ($windowStatus window)",
4683+
({ windowStatus, expectedClass, expectedMessage }) =>
4684+
Effect.gen(function* () {
4685+
const harness = yield* makeWakeHarness;
4686+
yield* harness.runtime.startTurn(
4687+
makeClaudeTestTurnInput({
4688+
threadId: harness.threadId,
4689+
providerThread: harness.providerThread,
4690+
now: yield* DateTime.now,
4691+
attemptId: RunAttemptId.make(`attempt-blocking-limit-${windowStatus}`),
4692+
text: "Continue.",
4693+
attachments: [],
4694+
}),
4695+
);
4696+
yield* Queue.offerAll(harness.sdkMessages, [
4697+
claudeSdkFrame({
4698+
type: "rate_limit_event",
4699+
rate_limit_info: { status: windowStatus, rateLimitType: "five_hour" },
4700+
uuid: "00000000-0000-4000-8000-000000000660",
4701+
session_id: WAKE_NATIVE_SESSION,
4702+
}),
4703+
makeAssistantErrorFrame({
4704+
uuid: "00000000-0000-4000-8000-000000000661",
4705+
error: "invalid_request",
4706+
text: "Prompt is too long",
4707+
}),
4708+
makeResultFrame({
4709+
uuid: "00000000-0000-4000-8000-000000000662",
4710+
result: "Prompt is too long",
4711+
isError: true,
4712+
numTurns: 189,
4713+
terminalReason: "blocking_limit",
4714+
}),
4715+
]);
4716+
const terminal = yield* Queue.take(harness.terminalReceipts);
4717+
assert.equal(terminal.status, "failed");
4718+
if (terminal.status !== "failed") return;
4719+
assert.equal(terminal.failure.class, expectedClass);
4720+
assert.include(terminal.failure.message, expectedMessage);
4721+
assert.equal(terminal.failure.code, "blocking_limit");
4722+
if (expectedClass === "provider_error") {
4723+
assert.include(terminal.failure.message, "/compact");
4724+
}
4725+
}).pipe(
4726+
Effect.provide(
4727+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4728+
),
4729+
),
4730+
);
4731+
46654732
const providerThreadRosterEvents = (
46664733
events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>,
46674734
) =>

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,9 @@ function resultUserFacingError(result: SDKResultMessage): string | undefined {
26182618
);
26192619
}
26202620

2621+
const CONTEXT_WINDOW_FULL_ERROR =
2622+
"Claude stopped: the prompt exceeds the model's context window. Run /compact or start a new thread.";
2623+
26212624
function terminalResultError(
26222625
reason: SDKResultMessage["terminal_reason"],
26232626
failureHint?: string,
@@ -2636,11 +2639,13 @@ function terminalResultError(
26362639
case "turn_setup_failed":
26372640
return "Claude could not start the turn.";
26382641
case "blocking_limit":
2639-
return "Claude stopped: a usage limit blocked the request.";
2642+
// The CLI also ends a turn with blocking_limit once the context window
2643+
// is full ("Prompt is too long"). Only rate-limit evidence sets the hint.
2644+
return failureHint ?? CONTEXT_WINDOW_FULL_ERROR;
26402645
case "rapid_refill_breaker":
26412646
return "Claude stopped: the context refilled too quickly after compaction.";
26422647
case "prompt_too_long":
2643-
return "Claude stopped: the prompt exceeds the model's context window.";
2648+
return CONTEXT_WINDOW_FULL_ERROR;
26442649
case "image_error":
26452650
return "Claude stopped: an image in the conversation could not be processed.";
26462651
case "model_error":
@@ -2817,9 +2822,7 @@ function providerFailureFromResult(
28172822
}
28182823
}
28192824
const failureClass =
2820-
message.terminal_reason === "blocking_limit" ||
2821-
(message.subtype === "success" && message.api_error_status === 429) ||
2822-
usageLimited
2825+
(message.subtype === "success" && message.api_error_status === 429) || usageLimited
28232826
? "usage_limit"
28242827
: "provider_error";
28252828
const listedError = resultUserFacingError(message);

0 commit comments

Comments
 (0)