The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(server): a /compact stopped by the Claude usage limit now fails (#19583)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Scott Norteman committed
Oct 18, 2026 at 15:13 UTC
499c9ee68b28492c9e088a98f112998dfc4ffafc
2 files changed
+130
-2
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+98
@@ -3385,6 +3385,104 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3385
),
3386
);
3387
3388
+ it.effect.each([
3389
+ {
3390
+ name: "fails a /compact the usage limit stopped",
3391
+ limited: true,
3392
+ compaction: "failed",
3393
+ compact: true,
3394
+ status: "failed",
3395
+ },
3396
+ {
3397
+ name: "completes an unlimited failed /compact",
3398
+ limited: false,
3399
+ compaction: "failed",
3400
+ compact: true,
3401
+ status: "completed",
3402
+ },
3403
+ {
3404
+ name: "completes a successful limited /compact",
3405
+ limited: true,
3406
+ compaction: "success",
3407
+ compact: true,
3408
+ status: "completed",
3409
+ },
3410
+ {
3411
+ name: "completes a failed automatic compaction",
3412
+ limited: true,
3413
+ compaction: "failed",
3414
+ compact: false,
3415
+ status: "completed",
3416
+ },
3417
+ ] as const)("$name", ({ name, limited, compaction, compact, status }) =>
3418
+ Effect.gen(function* () {
3419
+ const harness = yield* makeWakeHarness;
3420
+ const now = yield* DateTime.now;
3421
+ const resetsAt = 1791991200;
3422
+ const compactError =
3423
+ "Error during compaction: You've hit your session limit · resets 5:20pm (Europe/Berlin).";
3424
+ yield* harness.runtime.startTurn(
3425
+ makeClaudeTestTurnInput({
3426
+ threadId: harness.threadId,
3427
+ providerThread: harness.providerThread,
3428
+ now,
3429
+ attemptId: RunAttemptId.make(`attempt-claude-compaction-${name}`),
3430
+ text: compact ? "/compact" : "Continue.",
3431
+ attachments: [],
3432
+ }),
3433
+ );
3434
+ // Claude reports a compaction's outcome on a status frame. A /compact
3435
+ // then ends with a success result naming the command and no terminal
3436
+ // reason, even when the compaction failed (#19327). The automatic case
3437
+ // omits the terminal reason too, so only the missing command keeps it
3438
+ // completed.
3439
+ const result = makeResultFrame({
3440
+ uuid: "00000000-0000-4000-8000-000000000613",
3441
+ result: compact ? "" : "Done.",
3442
+ numTurns: compact ? 0 : 1,
3443
+ terminalReason: null,
3444
+ });
3445
+ yield* Queue.offerAll(harness.sdkMessages, [
3446
+ ...(limited
3447
+ ? [
3448
+ claudeSdkFrame({
3449
+ type: "rate_limit_event",
3450
+ rate_limit_info: { status: "rejected", rateLimitType: "five_hour", resetsAt },
3451
+ uuid: "00000000-0000-4000-8000-000000000611",
3452
+ session_id: WAKE_NATIVE_SESSION,
3453
+ }),
3454
+ ]
3455
+ : []),
3456
+ claudeSdkFrame({
3457
+ type: "system",
3458
+ subtype: "status",
3459
+ status: null,
3460
+ compact_result: compaction,
3461
+ ...(compaction === "failed"
3462
+ ? { compact_error: limited ? compactError : "Not enough messages to compact." }
3463
+ : {}),
3464
+ uuid: "00000000-0000-4000-8000-000000000612",
3465
+ session_id: WAKE_NATIVE_SESSION,
3466
+ }),
3467
+ compact ? claudeSdkFrame({ ...result, local_command: "compact" }) : result,
3468
+ ]);
3469
+
3470
+ const terminal = yield* Queue.take(harness.terminalReceipts);
3471
+ assert.equal(terminal.status, status);
3472
+ if (terminal.status !== "failed") return;
3473
+ assert.equal(terminal.failure.class, "usage_limit");
3474
+ assert.equal(terminal.failure.message, compactError);
3475
+ assert.equal(
3476
+ terminal.failure.resetAt,
3477
+ DateTime.formatIso(DateTime.makeUnsafe(resetsAt * 1000)),
3478
+ );
3479
+ }).pipe(
3480
+ Effect.provide(
3481
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3482
+ ),
3483
+ ),
3484
+ );
3485
+
3486
it.effect.each([
3487
{ name: "parent limit", parentErrors: ["rate_limit"], expectedLimit: true },
3488
{
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+32
-2
@@ -2997,6 +2997,9 @@ interface ActiveClaudeTurnContext {
2997
readonly rejectedRateLimitTypes: Set<string>;
2998
readonly rateLimitResetTimes: Map<string, string | null>;
2999
latestAssistantRateLimited: boolean;
3000
+ // Set while this turn's latest compaction status reports a failure, with
3001
+ // Claude's error text when it sent one.
3002
+ compactionFailure: { readonly message: string | undefined } | null;
3003
// Usage of the results that steers ended, oldest first. The final result's
3004
// usage covers only its own segment, so the turn bills these as well.
3005
readonly steeredSegmentUsage: Array<SDKResultMessage["usage"]>;
@@ -7010,6 +7013,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7013
}
7014
}
7015
7016
+ if (
7017
+ message.type === "system" &&
7018
+ message.subtype === "status" &&
7019
+ message.compact_result !== undefined
7020
+ ) {
7021
+ context.compactionFailure =
7022
+ message.compact_result === "failed" ? { message: message.compact_error } : null;
7023
+ }
7024
+
7025
if (message.type === "system" && message.subtype === "compact_boundary") {
7026
const now = yield* DateTime.now;
7027
const nativeItemId = message.uuid;
@@ -7749,6 +7761,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7761
(usageLimited
7762
? "Claude usage limit reached. Send the message again once the limit resets."
7763
: undefined));
7764
+ // A /compact the usage limit stopped still ends in a success
7765
+ // result. Failing the turn lets the usage-limit guard hold queued
7766
+ // messages instead of sending them into the same limit (#19327).
7767
+ const limitedCompaction =
7768
+ usageLimited && message.subtype === "success" && message.local_command === "compact"
7769
+ ? context.compactionFailure
7770
+ : null;
7771
const resetTimes = Array.from(context.rateLimitResetTimes.values());
7772
const resetAt =
7773
resetTimes.length > 0 && resetTimes.every((time) => time !== null)
@@ -7756,14 +7775,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7775
: null;
7776
const resultFailure = interrupted
7777
? null
7759
- : providerFailureFromResult(message, completedAt, failureHint, usageLimited);
7778
+ : limitedCompaction !== null
7779
+ ? makeProviderFailure({
7780
+ class: "usage_limit",
7781
+ message: limitedCompaction.message ?? failureHint,
7782
+ code: "compaction_failed",
7783
+ })
7784
+ : providerFailureFromResult(message, completedAt, failureHint, usageLimited);
7785
const terminalFailure =
7786
resultFailure?.class === "usage_limit"
7787
? { ...resultFailure, resetAt: resetAt ?? resultFailure.resetAt ?? null }
7788
: resultFailure;
7789
yield* finalizeActiveTurn({
7790
context,
7766
- status: interrupted ? "interrupted" : terminalStatusFromResult(message, failureHint),
7791
+ status: interrupted
7792
+ ? "interrupted"
7793
+ : limitedCompaction !== null
7794
+ ? "failed"
7795
+ : terminalStatusFromResult(message, failureHint),
7796
completedAt,
7797
result: message,
7798
...(terminalFailure === null ? {} : { failure: terminalFailure }),
@@ -8586,6 +8615,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8615
rejectedRateLimitTypes: new Set(),
8616
rateLimitResetTimes: new Map(),
8617
latestAssistantRateLimited: false,
8618
+ compactionFailure: null,
8619
steeredSegmentUsage: [],
8620
subagentsByTaskId: new Map(),
8621
subagentsByToolUseId: new Map(),