Skip to content

Commit fb9af9e

Browse files
authored
fix(server): a full Claude context window no longer shows as a usage limit (pingdotgg#19848)
1 parent 3ec095b commit fb9af9e

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
@@ -2299,7 +2299,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22992299
});
23002300
const makeAssistantErrorFrame = (input: {
23012301
readonly uuid: string;
2302-
readonly error: "authentication_failed" | "rate_limit" | "server_error" | undefined;
2302+
readonly error:
2303+
| "authentication_failed"
2304+
| "invalid_request"
2305+
| "rate_limit"
2306+
| "server_error"
2307+
| undefined;
2308+
readonly text?: string;
23032309
readonly parentToolUseId?: string | null;
23042310
}) =>
23052311
claudeSdkFrame({
@@ -2309,7 +2315,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
23092315
id: `msg_${input.uuid}`,
23102316
type: "message",
23112317
role: "assistant",
2312-
content: [{ type: "text", text: "Claude could not complete this request." }],
2318+
content: [{ type: "text", text: input.text ?? "Claude could not complete this request." }],
23132319
stop_reason: null,
23142320
stop_sequence: null,
23152321
usage: {
@@ -5295,10 +5301,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
52955301
assert.equal(terminal.status, "failed");
52965302
if (terminal.status !== "failed") return;
52975303
assert.isNotEmpty(terminal.failure.message);
5298-
assert.equal(
5299-
terminal.failure.class,
5300-
terminalReason === "blocking_limit" ? "usage_limit" : "provider_error",
5301-
);
5304+
assert.equal(terminal.failure.class, "provider_error");
53025305
assert.isFalse(
53035306
harness.events.some(
53045307
(event) =>
@@ -5318,6 +5321,70 @@ describe("ClaudeAdapterV2 background wake turns", () => {
53185321
),
53195322
);
53205323

5324+
// Frames follow #19832: a full context window ends the turn with
5325+
// blocking_limit while the plan's rate-limit window is still allowed.
5326+
it.effect.each([
5327+
{
5328+
windowStatus: "allowed",
5329+
expectedClass: "provider_error",
5330+
expectedMessage: "Claude stopped: the prompt exceeds the model's context window.",
5331+
},
5332+
{
5333+
windowStatus: "rejected",
5334+
expectedClass: "usage_limit",
5335+
expectedMessage: "Claude usage limit reached. Send the message again once the limit resets.",
5336+
},
5337+
] as const)(
5338+
"classifies blocking_limit by its rate-limit evidence ($windowStatus window)",
5339+
({ windowStatus, expectedClass, expectedMessage }) =>
5340+
Effect.gen(function* () {
5341+
const harness = yield* makeWakeHarness;
5342+
yield* harness.runtime.startTurn(
5343+
makeClaudeTestTurnInput({
5344+
threadId: harness.threadId,
5345+
providerThread: harness.providerThread,
5346+
now: yield* DateTime.now,
5347+
attemptId: RunAttemptId.make(`attempt-blocking-limit-${windowStatus}`),
5348+
text: "Continue.",
5349+
attachments: [],
5350+
}),
5351+
);
5352+
yield* Queue.offerAll(harness.sdkMessages, [
5353+
claudeSdkFrame({
5354+
type: "rate_limit_event",
5355+
rate_limit_info: { status: windowStatus, rateLimitType: "five_hour" },
5356+
uuid: "00000000-0000-4000-8000-000000000660",
5357+
session_id: WAKE_NATIVE_SESSION,
5358+
}),
5359+
makeAssistantErrorFrame({
5360+
uuid: "00000000-0000-4000-8000-000000000661",
5361+
error: "invalid_request",
5362+
text: "Prompt is too long",
5363+
}),
5364+
makeResultFrame({
5365+
uuid: "00000000-0000-4000-8000-000000000662",
5366+
result: "Prompt is too long",
5367+
isError: true,
5368+
numTurns: 189,
5369+
terminalReason: "blocking_limit",
5370+
}),
5371+
]);
5372+
const terminal = yield* Queue.take(harness.terminalReceipts);
5373+
assert.equal(terminal.status, "failed");
5374+
if (terminal.status !== "failed") return;
5375+
assert.equal(terminal.failure.class, expectedClass);
5376+
assert.include(terminal.failure.message, expectedMessage);
5377+
assert.equal(terminal.failure.code, "blocking_limit");
5378+
if (expectedClass === "provider_error") {
5379+
assert.include(terminal.failure.message, "/compact");
5380+
}
5381+
}).pipe(
5382+
Effect.provide(
5383+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5384+
),
5385+
),
5386+
);
5387+
53215388
const providerThreadRosterEvents = (
53225389
events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>,
53235390
) =>

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,9 @@ function resultUserFacingError(result: SDKResultMessage): string | undefined {
26332633
);
26342634
}
26352635

2636+
const CONTEXT_WINDOW_FULL_ERROR =
2637+
"Claude stopped: the prompt exceeds the model's context window. Run /compact or start a new thread.";
2638+
26362639
function terminalResultError(
26372640
reason: SDKResultMessage["terminal_reason"],
26382641
failureHint?: string,
@@ -2651,11 +2654,13 @@ function terminalResultError(
26512654
case "turn_setup_failed":
26522655
return "Claude could not start the turn.";
26532656
case "blocking_limit":
2654-
return "Claude stopped: a usage limit blocked the request.";
2657+
// The CLI also ends a turn with blocking_limit once the context window
2658+
// is full ("Prompt is too long"). Only rate-limit evidence sets the hint.
2659+
return failureHint ?? CONTEXT_WINDOW_FULL_ERROR;
26552660
case "rapid_refill_breaker":
26562661
return "Claude stopped: the context refilled too quickly after compaction.";
26572662
case "prompt_too_long":
2658-
return "Claude stopped: the prompt exceeds the model's context window.";
2663+
return CONTEXT_WINDOW_FULL_ERROR;
26592664
case "image_error":
26602665
return "Claude stopped: an image in the conversation could not be processed.";
26612666
case "model_error":
@@ -2822,9 +2827,7 @@ function providerFailureFromResult(
28222827
}
28232828
}
28242829
const failureClass =
2825-
message.terminal_reason === "blocking_limit" ||
2826-
(message.subtype === "success" && message.api_error_status === 429) ||
2827-
usageLimited
2830+
(message.subtype === "success" && message.api_error_status === 429) || usageLimited
28282831
? "usage_limit"
28292832
: "provider_error";
28302833
const listedError = resultUserFacingError(message);

0 commit comments

Comments
 (0)