@devries-cameron20 / t3code-1 / commits / e094c68d0a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): preserve buffered Claude usage-limit failures (#19543)

Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Omar committed Oct 20, 2026 at 01:06 UTC e094c68d0aec702566e3822999a2a520eb91bf89
2 files changed +145 -93
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+126 -88
@@ -8215,6 +8215,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
8215 );
8216 assert.lengthOf(harness.continuationRequests, 1);
8217
8218 + // An empty session-task lifecycle result arriving before T3 opens the
8219 + // wake run must not replace the root's terminal usage-limit result.
8220 + yield* harness.offerAndWait(wakeTurnInit);
8221 + yield* harness.offerAndWait(
8222 + makeResultFrame({
8223 + uuid: "00000000-0000-4000-8000-000000000633",
8224 + result: "",
8225 + numTurns: 0,
8226 + origin: { kind: "task-notification" },
8227 + terminalReason: null,
8228 + }),
8229 + );
8230 +
8231 yield* harness.runtime.startTurn(
8232 makeClaudeTestTurnInput({
8233 threadId: harness.threadId,
@@ -8555,7 +8568,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
8568 harness.sdkMessages,
8569 makeResultFrame({
8570 uuid: "00000000-0000-4000-8000-00000000010f",
8558 - result: "Wake result with no model turns.",
8571 + result: "",
8572 numTurns: 0,
8573 origin: { kind: "task-notification" },
8574 }),
@@ -8894,107 +8907,132 @@ describe("ClaudeAdapterV2 background wake turns", () => {
8907 return { childThreadId, toolThreadIds, assistantTexts };
8908 };
8909
8897 - it.effect("keeps the wake run open for the turn Claude opens right after an empty one", () =>
8898 - Effect.scoped(
8899 - Effect.gen(function* () {
8900 - const harness = yield* makeWakeHarness;
8901 - const now = yield* DateTime.now;
8902 - const subagents = [
8903 - { taskId: "task-wake-pair-a", toolUseId: "toolu_wake_pair_a" },
8904 - { taskId: "task-wake-pair-b", toolUseId: "toolu_wake_pair_b" },
8905 - ];
8906 - const wakeAttempt = RunAttemptId.make("attempt-claude-wake-pair-2");
8910 + it.effect.each([
8911 + { label: "an empty one", quotaFailureFirst: false },
8912 + { label: "a quota failure and an empty one", quotaFailureFirst: true },
8913 + ])(
8914 + "keeps the wake run open for the turn Claude opens right after $label",
8915 + ({ quotaFailureFirst }) =>
8916 + Effect.scoped(
8917 + Effect.gen(function* () {
8918 + const harness = yield* makeWakeHarness;
8919 + const now = yield* DateTime.now;
8920 + const subagents = [
8921 + { taskId: "task-wake-pair-a", toolUseId: "toolu_wake_pair_a" },
8922 + { taskId: "task-wake-pair-b", toolUseId: "toolu_wake_pair_b" },
8923 + ];
8924 + const wakeAttempt = RunAttemptId.make("attempt-claude-wake-pair-2");
8925
8908 - yield* harness.runtime.startTurn(
8909 - makeClaudeTestTurnInput({
8910 - threadId: harness.threadId,
8911 - providerThread: harness.providerThread,
8912 - now,
8913 - attemptId: RunAttemptId.make("attempt-claude-wake-pair-1"),
8914 - text: "Audit both in the background.",
8915 - attachments: [],
8916 - }),
8917 - );
8918 - for (const [index, subagent] of subagents.entries()) {
8926 + yield* harness.runtime.startTurn(
8927 + makeClaudeTestTurnInput({
8928 + threadId: harness.threadId,
8929 + providerThread: harness.providerThread,
8930 + now,
8931 + attemptId: RunAttemptId.make("attempt-claude-wake-pair-1"),
8932 + text: "Audit both in the background.",
8933 + attachments: [],
8934 + }),
8935 + );
8936 + for (const [index, subagent] of subagents.entries()) {
8937 + yield* Queue.offer(
8938 + harness.sdkMessages,
8939 + makeSubagentTaskStartedFrame({
8940 + ...subagent,
8941 + uuid: `00000000-0000-4000-8000-00000000095${index}`,
8942 + }),
8943 + );
8944 + }
8945 yield* Queue.offer(
8946 harness.sdkMessages,
8921 - makeSubagentTaskStartedFrame({
8922 - ...subagent,
8923 - uuid: `00000000-0000-4000-8000-00000000095${index}`,
8924 - }),
8947 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000952", result: "Started." }),
8948 );
8926 - }
8927 - yield* Queue.offer(
8928 - harness.sdkMessages,
8929 - makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000952", result: "Started." }),
8930 - );
8931 - yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
8949 + yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
8950
8933 - // Both subagents end together. Claude runs an empty turn for the first
8934 - // notification and opens the real one a millisecond later, all before
8935 - // the wake run attaches.
8936 - for (const [index, subagent] of subagents.entries()) {
8951 + // Both subagents end together. Claude runs an empty turn for the first
8952 + // notification and opens the real one a millisecond later, all before
8953 + // the wake run attaches.
8954 + for (const [index, subagent] of subagents.entries()) {
8955 + yield* Queue.offer(
8956 + harness.sdkMessages,
8957 + makeSubagentNotificationFrame({
8958 + ...subagent,
8959 + summary: "AUDITED",
8960 + uuid: `00000000-0000-4000-8000-00000000096${index}`,
8961 + }),
8962 + );
8963 + }
8964 + // A rejected wake that precedes the empty turn still must not settle
8965 + // the run the live turn owns.
8966 + if (quotaFailureFirst) {
8967 + yield* harness.offerAndWait(
8968 + makeAssistantErrorFrame({
8969 + uuid: "00000000-0000-4000-8000-000000000964",
8970 + error: "rate_limit",
8971 + }),
8972 + );
8973 + yield* harness.offerAndWait(
8974 + makeResultFrame({
8975 + uuid: "00000000-0000-4000-8000-000000000965",
8976 + result: "You've hit your session limit · resets 11:10am (Australia/Sydney)",
8977 + isError: true,
8978 + apiErrorStatus: 429,
8979 + terminalReason: "api_error",
8980 + origin: { kind: "task-notification" },
8981 + }),
8982 + );
8983 + }
8984 + yield* Queue.offer(harness.sdkMessages, wakeTurnInit);
8985 yield* Queue.offer(
8986 harness.sdkMessages,
8939 - makeSubagentNotificationFrame({
8940 - ...subagent,
8941 - summary: "AUDITED",
8942 - uuid: `00000000-0000-4000-8000-00000000096${index}`,
8987 + makeResultFrame({
8988 + uuid: "00000000-0000-4000-8000-000000000962",
8989 + result: "",
8990 + numTurns: 0,
8991 + origin: { kind: "task-notification" },
8992 + terminalReason: null,
8993 }),
8994 );
8945 - }
8946 - yield* Queue.offer(harness.sdkMessages, wakeTurnInit);
8947 - yield* Queue.offer(
8948 - harness.sdkMessages,
8949 - makeResultFrame({
8950 - uuid: "00000000-0000-4000-8000-000000000962",
8951 - result: "",
8952 - numTurns: 0,
8953 - origin: { kind: "task-notification" },
8954 - terminalReason: null,
8955 - }),
8956 - );
8957 - yield* harness.offerAndWait(
8958 - claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000963" }),
8959 - );
8960 - assert.lengthOf(harness.continuationRequests, 1);
8995 + yield* harness.offerAndWait(
8996 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000963" }),
8997 + );
8998 + assert.lengthOf(harness.continuationRequests, 1);
8999
8962 - yield* harness.runtime.startTurn(
8963 - makeClaudeTestTurnInput({
8964 - threadId: harness.threadId,
8965 - providerThread: harness.providerThread,
8966 - now,
8967 - attemptId: wakeAttempt,
8968 - text: "Background task completed.",
8969 - attachments: [],
8970 - providerTurnOrdinal: 2,
8971 - messageCreatedBy: "agent",
8972 - messageCreationSource: "provider",
8973 - }),
8974 - );
8975 - // Claude is still thinking, so the run stays working rather than
8976 - // settling with nothing to show.
8977 - assert.lengthOf(harness.terminalEvents(), 1);
9000 + yield* harness.runtime.startTurn(
9001 + makeClaudeTestTurnInput({
9002 + threadId: harness.threadId,
9003 + providerThread: harness.providerThread,
9004 + now,
9005 + attemptId: wakeAttempt,
9006 + text: "Background task completed.",
9007 + attachments: [],
9008 + providerTurnOrdinal: 2,
9009 + messageCreatedBy: "agent",
9010 + messageCreationSource: "provider",
9011 + }),
9012 + );
9013 + // Claude is still thinking, so the run stays working rather than
9014 + // settling with nothing to show.
9015 + assert.lengthOf(harness.terminalEvents(), 1);
9016
8979 - yield* harness.offerAndWait(wakeAssistant);
8980 - yield* harness.offerAndWait(wakeResult);
8981 - yield* awaitUntil(() => harness.terminalEvents().length === 2, "wake run terminal");
8982 - assert.equal(harness.terminalEvents()[1]?.status, "completed");
8983 - assert.lengthOf(harness.continuationRequests, 1);
8984 - assert.isTrue(
8985 - harness.events.some(
8986 - (event) =>
8987 - event.type === "message.updated" &&
8988 - event.message.runId === RunId.make(`run-${wakeAttempt}`) &&
8989 - event.message.text === WAKE_RESULT_TEXT,
9017 + yield* harness.offerAndWait(wakeAssistant);
9018 + yield* harness.offerAndWait(wakeResult);
9019 + yield* awaitUntil(() => harness.terminalEvents().length === 2, "wake run terminal");
9020 + assert.equal(harness.terminalEvents()[1]?.status, "completed");
9021 + assert.lengthOf(harness.continuationRequests, 1);
9022 + assert.isTrue(
9023 + harness.events.some(
9024 + (event) =>
9025 + event.type === "message.updated" &&
9026 + event.message.runId === RunId.make(`run-${wakeAttempt}`) &&
9027 + event.message.text === WAKE_RESULT_TEXT,
9028 + ),
9029 + );
9030 + }).pipe(
9031 + Effect.provide(
9032 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9033 ),
8991 - );
8992 - }).pipe(
8993 - Effect.provide(
8994 - Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9034 ),
9035 ),
8997 - ),
9036 );
9037
9038 it.effect("a subagent re-run in the foreground does not join a later wake", () =>
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+19 -5
@@ -9181,15 +9181,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9181 // the next within a millisecond; that next turn is still running
9182 // and owns this run, so the empty turn's result must not settle it.
9183 const lastResultIndex = drained.findLastIndex((entry) => entry.type === "result");
9184 - const lastResult = drained[lastResultIndex];
9184 + const tailResult = drained[lastResultIndex];
9185 const emptyTurnBeforeNextTurn =
9186 - lastResult?.type === "result" &&
9187 - lastResult.num_turns === 0 &&
9188 - !lastResult.is_error &&
9189 - terminalStatusFromResult(lastResult) === "completed" &&
9186 + tailResult?.type === "result" &&
9187 + tailResult.num_turns === 0 &&
9188 + !tailResult.is_error &&
9189 + terminalStatusFromResult(tailResult) === "completed" &&
9190 drained
9191 .slice(lastResultIndex + 1)
9192 .some((entry) => isClaudeTurnStartMessage(entry));
9193 + // The CLI can append empty task successes after a failure. Filter
9194 + // those for replay without changing the raw-tail successor guard.
9195 + const isEmptyTaskNotificationSuccess = (entry: SDKMessage) =>
9196 + isClaudeTaskNotificationOriginResult(entry) &&
9197 + entry.subtype === "success" &&
9198 + !entry.is_error &&
9199 + entry.num_turns === 0 &&
9200 + entry.result.length === 0 &&
9201 + terminalStatusFromResult(entry) === "completed";
9202 + const replayResultIndex = drained.findLastIndex(
9203 + (entry) => entry.type === "result" && !isEmptyTaskNotificationSuccess(entry),
9204 + );
9205 + const lastResult =
9206 + drained[replayResultIndex === -1 ? lastResultIndex : replayResultIndex];
9207 const opaqueReplayTombstones = taskIdSetForNativeThread(
9208 yield* Ref.get(opaqueBackgroundTaskReplayTombstonesByNativeThread),
9209 nativeThreadId,