The best way to control Claude Code, Codex and more from any computer or phone
fix(server): anchor Claude wake cooldowns to result arrival (#25061)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 18:31 UTC
371e9451ef2704271652c46490ca813d2bc41927
2 files changed
+96
-4
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+82
@@ -11254,6 +11254,88 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11254
),
11255
);
11256
11257
+ it.effect(
11258
+ "dates a buffered wake gateway limit from its arrival, not the continuation replay",
11259
+ () =>
11260
+ Effect.scoped(
11261
+ Effect.gen(function* () {
11262
+ const harness = yield* makeWakeHarness;
11263
+ const now = yield* DateTime.now;
11264
+
11265
+ yield* harness.runtime.startTurn(
11266
+ makeClaudeTestTurnInput({
11267
+ threadId: harness.threadId,
11268
+ providerThread: harness.providerThread,
11269
+ now,
11270
+ attemptId: RunAttemptId.make("attempt-claude-wake-gateway-1"),
11271
+ text: "Run the build in the background.",
11272
+ attachments: [],
11273
+ }),
11274
+ );
11275
+ yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
11276
+ yield* Queue.offer(harness.sdkMessages, turnOneResult);
11277
+ yield* Queue.take(harness.terminalReceipts);
11278
+
11279
+ yield* Queue.offer(harness.sdkMessages, wakeNotification);
11280
+ yield* harness.offerAndWait(
11281
+ makeAssistantErrorFrame({
11282
+ uuid: "00000000-0000-4000-8000-000000000634",
11283
+ error: "server_error",
11284
+ }),
11285
+ );
11286
+ assert.lengthOf(harness.continuationRequests, 1);
11287
+ const arrivedAt = yield* DateTime.now;
11288
+ yield* harness.offerAndWait(
11289
+ makeResultFrame({
11290
+ uuid: "00000000-0000-4000-8000-000000000635",
11291
+ result:
11292
+ 'API Error: 503 [cpa/claude-opus-5-5] [429]: {"type":"error","error":{"type":"rate_limit_error","message":"All credentials are cooling down"}} (reset after 2h54m21s)',
11293
+ isError: true,
11294
+ apiErrorStatus: 503,
11295
+ terminalReason: "api_error",
11296
+ origin: { kind: "task-notification" },
11297
+ }),
11298
+ );
11299
+
11300
+ yield* TestClock.adjust("1 hour");
11301
+ yield* harness.runtime.startTurn(
11302
+ makeClaudeTestTurnInput({
11303
+ threadId: harness.threadId,
11304
+ providerThread: harness.providerThread,
11305
+ now: yield* DateTime.now,
11306
+ attemptId: RunAttemptId.make("attempt-claude-wake-gateway-2"),
11307
+ text: "Background task completed.",
11308
+ attachments: [],
11309
+ providerTurnOrdinal: 2,
11310
+ messageCreatedBy: "agent",
11311
+ messageCreationSource: "provider",
11312
+ }),
11313
+ );
11314
+ const terminal = yield* Queue.take(harness.terminalReceipts);
11315
+ assert.equal(terminal.status, "failed");
11316
+ if (terminal.status !== "failed") return;
11317
+ assert.equal(terminal.failure.class, "usage_limit");
11318
+ assert.equal(
11319
+ terminal.failure.resetAt,
11320
+ DateTime.formatIso(
11321
+ DateTime.makeUnsafe(
11322
+ DateTime.toEpochMillis(arrivedAt) + (2 * 3_600 + 54 * 60 + 21) * 1_000,
11323
+ ),
11324
+ ),
11325
+ );
11326
+ }).pipe(
11327
+ Effect.provide(
11328
+ Layer.mergeAll(
11329
+ IdAllocator.layer,
11330
+ McpProviderSessions.layer,
11331
+ NodeServices.layer,
11332
+ NodeCrypto.layer,
11333
+ ),
11334
+ ),
11335
+ ),
11336
+ ),
11337
+ );
11338
+
11339
it.effect("leaves buffered wake messages for the continuation queued behind a user turn", () =>
11340
Effect.scoped(
11341
Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+14
-4
@@ -3800,7 +3800,7 @@ function gatewayCooldownResetAt(
3800
3801
function providerFailureFromResult(
3802
message: SDKResultMessage,
3803
- completedAt: DateTime.Utc,
3803
+ receivedAt: DateTime.Utc,
3804
failureHint?: string,
3805
usageLimited = false,
3806
): OrchestrationV2ProviderFailure | null {
@@ -3814,7 +3814,7 @@ function providerFailureFromResult(
3814
/^API Error: 503 \[[^\]\r\n]+\] \[429\]: \{"type":"error","error":\{"type":"rate_limit_error",[^\r\n]*\} \(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/.exec(
3815
message.result,
3816
),
3817
- completedAt,
3817
+ receivedAt,
3818
)
3819
: undefined;
3820
if (gatewayResetAt !== undefined) {
@@ -3858,7 +3858,7 @@ function providerFailureFromResult(
3858
apiErrorStatus === 429 && message.result.startsWith("API Error:")
3859
? gatewayCooldownResetAt(
3860
/\(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/.exec(message.result),
3861
- completedAt,
3861
+ receivedAt,
3862
)
3863
: undefined;
3864
return makeProviderFailure({
@@ -4882,6 +4882,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
4882
const pendingSubagentFramesByToolUseId = yield* Ref.make(
4883
new Map<string, ReadonlyArray<SDKMessage>>(),
4884
);
4885
+ // A gateway's relative reset counts from the result's arrival, not its replay.
4886
+ const bufferedResultArrivals = new WeakMap<SDKMessage, DateTime.Utc>();
4887
const wakeBuffers = yield* Ref.make(
4888
new Map<
4889
string,
@@ -8065,6 +8067,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8067
childThreadId: registered?.childThreadId,
8068
});
8069
}
8070
+ if (message.type === "result") {
8071
+ bufferedResultArrivals.set(message, yield* DateTime.now);
8072
+ }
8073
yield* Ref.update(wakeBuffers, (current) => {
8074
const existing = current.get(wakeInput.nativeThreadId);
8075
const updated = new Map(current);
@@ -9535,7 +9540,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9540
message: limitedCompaction.message ?? failureHint,
9541
code: "compaction_failed",
9542
})
9538
- : providerFailureFromResult(message, completedAt, failureHint, usageLimited);
9543
+ : providerFailureFromResult(
9544
+ message,
9545
+ bufferedResultArrivals.get(message) ?? completedAt,
9546
+ failureHint,
9547
+ usageLimited,
9548
+ );
9549
const terminalFailure =
9550
resultFailure?.class === "usage_limit"
9551
? { ...resultFailure, resetAt: resetAt ?? resultFailure.resetAt ?? null }