Skip to content

Commit e86fb75

Browse files
ScottN-PVclaude
andcommitted
fix(server): a /compact stopped by the Claude usage limit now fails
Claude ends a /compact with a success result even when the compaction failed on a rejected usage window, so the run was stored as completed and the usage-limit guard let queued messages start into the same limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bf8ffe8 commit e86fb75

2 files changed

Lines changed: 130 additions & 2 deletions

File tree

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,6 +3399,104 @@ describe("ClaudeAdapterV2 background wake turns", () => {
33993399
),
34003400
);
34013401

3402+
it.effect.each([
3403+
{
3404+
name: "fails a /compact the usage limit stopped",
3405+
limited: true,
3406+
compaction: "failed",
3407+
compact: true,
3408+
status: "failed",
3409+
},
3410+
{
3411+
name: "completes an unlimited failed /compact",
3412+
limited: false,
3413+
compaction: "failed",
3414+
compact: true,
3415+
status: "completed",
3416+
},
3417+
{
3418+
name: "completes a successful limited /compact",
3419+
limited: true,
3420+
compaction: "success",
3421+
compact: true,
3422+
status: "completed",
3423+
},
3424+
{
3425+
name: "completes a failed automatic compaction",
3426+
limited: true,
3427+
compaction: "failed",
3428+
compact: false,
3429+
status: "completed",
3430+
},
3431+
] as const)("$name", ({ name, limited, compaction, compact, status }) =>
3432+
Effect.gen(function* () {
3433+
const harness = yield* makeWakeHarness;
3434+
const now = yield* DateTime.now;
3435+
const resetsAt = 1791991200;
3436+
const compactError =
3437+
"Error during compaction: You've hit your session limit · resets 5:20pm (Europe/Berlin).";
3438+
yield* harness.runtime.startTurn(
3439+
makeClaudeTestTurnInput({
3440+
threadId: harness.threadId,
3441+
providerThread: harness.providerThread,
3442+
now,
3443+
attemptId: RunAttemptId.make(`attempt-claude-compaction-${name}`),
3444+
text: compact ? "/compact" : "Continue.",
3445+
attachments: [],
3446+
}),
3447+
);
3448+
// Claude reports a compaction's outcome on a status frame. A /compact
3449+
// then ends with a success result naming the command and no terminal
3450+
// reason, even when the compaction failed (#19327). The automatic case
3451+
// omits the terminal reason too, so only the missing command keeps it
3452+
// completed.
3453+
const result = makeResultFrame({
3454+
uuid: "00000000-0000-4000-8000-000000000613",
3455+
result: compact ? "" : "Done.",
3456+
numTurns: compact ? 0 : 1,
3457+
terminalReason: null,
3458+
});
3459+
yield* Queue.offerAll(harness.sdkMessages, [
3460+
...(limited
3461+
? [
3462+
claudeSdkFrame({
3463+
type: "rate_limit_event",
3464+
rate_limit_info: { status: "rejected", rateLimitType: "five_hour", resetsAt },
3465+
uuid: "00000000-0000-4000-8000-000000000611",
3466+
session_id: WAKE_NATIVE_SESSION,
3467+
}),
3468+
]
3469+
: []),
3470+
claudeSdkFrame({
3471+
type: "system",
3472+
subtype: "status",
3473+
status: null,
3474+
compact_result: compaction,
3475+
...(compaction === "failed"
3476+
? { compact_error: limited ? compactError : "Not enough messages to compact." }
3477+
: {}),
3478+
uuid: "00000000-0000-4000-8000-000000000612",
3479+
session_id: WAKE_NATIVE_SESSION,
3480+
}),
3481+
compact ? claudeSdkFrame({ ...result, local_command: "compact" }) : result,
3482+
]);
3483+
3484+
const terminal = yield* Queue.take(harness.terminalReceipts);
3485+
assert.equal(terminal.status, status);
3486+
if (terminal.status !== "failed") return;
3487+
assert.equal(terminal.failure.class, "usage_limit");
3488+
assert.equal(terminal.failure.message, compactError);
3489+
assert.equal(
3490+
terminal.failure.resetAt,
3491+
DateTime.formatIso(DateTime.makeUnsafe(resetsAt * 1000)),
3492+
);
3493+
}).pipe(
3494+
Effect.provide(
3495+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3496+
),
3497+
),
3498+
);
3499+
34023500
it.effect.each([
34033501
{ name: "parent limit", parentErrors: ["rate_limit"], expectedLimit: true },
34043502
{

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,6 +3046,9 @@ interface ActiveClaudeTurnContext {
30463046
readonly rejectedRateLimitTypes: Set<string>;
30473047
readonly rateLimitResetTimes: Map<string, string | null>;
30483048
latestAssistantRateLimited: boolean;
3049+
// Set while this turn's latest compaction status reports a failure, with
3050+
// Claude's error text when it sent one.
3051+
compactionFailure: { readonly message: string | undefined } | null;
30493052
// Usage of the results that steers ended, oldest first. The final result's
30503053
// usage covers only its own segment, so the turn bills these as well.
30513054
readonly steeredSegmentUsage: Array<SDKResultMessage["usage"]>;
@@ -7035,6 +7038,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
70357038
}
70367039
}
70377040

7041+
if (
7042+
message.type === "system" &&
7043+
message.subtype === "status" &&
7044+
message.compact_result !== undefined
7045+
) {
7046+
context.compactionFailure =
7047+
message.compact_result === "failed" ? { message: message.compact_error } : null;
7048+
}
7049+
70387050
if (message.type === "system" && message.subtype === "compact_boundary") {
70397051
const now = yield* DateTime.now;
70407052
const nativeItemId = message.uuid;
@@ -7847,21 +7859,38 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
78477859
(usageLimited
78487860
? "Claude usage limit reached. Send the message again once the limit resets."
78497861
: undefined));
7862+
// A /compact the usage limit stopped still ends in a success
7863+
// result. Failing the turn lets the usage-limit guard hold queued
7864+
// messages instead of sending them into the same limit (#19327).
7865+
const limitedCompaction =
7866+
usageLimited && message.subtype === "success" && message.local_command === "compact"
7867+
? context.compactionFailure
7868+
: null;
78507869
const resetTimes = Array.from(context.rateLimitResetTimes.values());
78517870
const resetAt =
78527871
resetTimes.length > 0 && resetTimes.every((time) => time !== null)
78537872
? resetTimes.reduce((latest, time) => (time! > latest ? time! : latest), "")
78547873
: null;
78557874
const resultFailure = interrupted
78567875
? null
7857-
: providerFailureFromResult(message, completedAt, failureHint, usageLimited);
7876+
: limitedCompaction !== null
7877+
? makeProviderFailure({
7878+
class: "usage_limit",
7879+
message: limitedCompaction.message ?? failureHint,
7880+
code: "compaction_failed",
7881+
})
7882+
: providerFailureFromResult(message, completedAt, failureHint, usageLimited);
78587883
const terminalFailure =
78597884
resultFailure?.class === "usage_limit"
78607885
? { ...resultFailure, resetAt: resetAt ?? resultFailure.resetAt ?? null }
78617886
: resultFailure;
78627887
yield* finalizeActiveTurn({
78637888
context,
7864-
status: interrupted ? "interrupted" : terminalStatusFromResult(message, failureHint),
7889+
status: interrupted
7890+
? "interrupted"
7891+
: limitedCompaction !== null
7892+
? "failed"
7893+
: terminalStatusFromResult(message, failureHint),
78657894
completedAt,
78667895
result: message,
78677896
...(terminalFailure === null ? {} : { failure: terminalFailure }),
@@ -8755,6 +8784,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
87558784
rejectedRateLimitTypes: new Set(),
87568785
rateLimitResetTimes: new Map(),
87578786
latestAssistantRateLimited: false,
8787+
compactionFailure: null,
87588788
steeredSegmentUsage: [],
87598789
subagentsByTaskId: new Map(),
87608790
subagentsByToolUseId: new Map(),

0 commit comments

Comments
 (0)