@devries-cameron20 / t3code-1 / commits / 265d68ab04
Fork 0

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

fix(server): reverting a Claude thread to a subagent-only turn no longer breaks it (#21151)

fixfon committed Oct 18, 2026 at 22:49 UTC 265d68ab0406b47cb680adfd889a38dac021d9c8
2 files changed +158 -1
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+156
@@ -26,6 +26,7 @@ import {
26 ChatAttachmentId,
27 ChatFileAttachment,
28 ChatImageAttachment,
29 + CheckpointId,
30 ClaudeSettings,
31 EnvironmentId,
32 MessageId,
@@ -3327,6 +3328,161 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3328 ),
3329 );
3330
3331 + const lastProviderTurn = (events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>) => {
3332 + const event = events.findLast((candidate) => candidate.type === "provider_turn.updated");
3333 + if (event?.type !== "provider_turn.updated") {
3334 + throw new Error("Expected a provider turn update.");
3335 + }
3336 + return event.providerTurn;
3337 + };
3338 + // Rollback closes the live query and waits for its message stream to end.
3339 + const makeRollbackHarness = makeWakeHarnessWithOptions({
3340 + close: (sdkMessages) => Queue.shutdown(sdkMessages),
3341 + });
3342 +
3343 + it.effect(
3344 + "rolls back to the main-thread assistant message when a subagent frame comes last",
3345 + () =>
3346 + Effect.gen(function* () {
3347 + const harness = yield* makeRollbackHarness;
3348 + const now = yield* DateTime.now;
3349 + const mainUuid = "00000000-0000-4000-8000-000000000641";
3350 + yield* harness.runtime.startTurn(
3351 + makeClaudeTestTurnInput({
3352 + threadId: harness.threadId,
3353 + providerThread: harness.providerThread,
3354 + now,
3355 + attemptId: RunAttemptId.make("attempt-claude-cursor-main"),
3356 + text: "Continue.",
3357 + attachments: [],
3358 + }),
3359 + );
3360 + yield* Queue.offerAll(harness.sdkMessages, [
3361 + makeAssistantTextFrame({ uuid: mainUuid, text: "Delegating." }),
3362 + makeAssistantErrorFrame({
3363 + uuid: "00000000-0000-4000-8000-000000000642",
3364 + error: undefined,
3365 + parentToolUseId: "toolu_subagent",
3366 + }),
3367 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000643", result: "Done." }),
3368 + ]);
3369 + yield* Queue.take(harness.terminalReceipts);
3370 +
3371 + const providerTurn = lastProviderTurn(harness.events);
3372 + assert.equal(providerTurn.nativeTurnRef?.nativeId, mainUuid);
3373 + const rolledBack = yield* harness.runtime.rollbackThread({
3374 + providerThread: harness.providerThread,
3375 + target: {
3376 + type: "provider_turn",
3377 + checkpointId: CheckpointId.make("checkpoint-claude-cursor-main"),
3378 + appRunOrdinal: 1,
3379 + providerTurn,
3380 + },
3381 + providerThreadTurns: [
3382 + providerTurn,
3383 + {
3384 + ...providerTurn,
3385 + id: ProviderTurnId.make("provider-turn-claude-cursor-next"),
3386 + ordinal: 2,
3387 + },
3388 + ],
3389 + });
3390 + assert.equal(rolledBack.providerThread.nativeConversationHeadRef?.nativeId, mainUuid);
3391 + }).pipe(
3392 + Effect.provide(
3393 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3394 + ),
3395 + ),
3396 + );
3397 +
3398 + it.effect("never records a subagent uuid for a turn with no main-thread assistant message", () =>
3399 + Effect.gen(function* () {
3400 + // The continuation turn reopens the query, and rollback shut the first queue down.
3401 + const harness = yield* makeWakeHarnessWithOptions({
3402 + close: (sdkMessages) => Queue.shutdown(sdkMessages),
3403 + freshQueueOnReopen: true,
3404 + });
3405 + const now = yield* DateTime.now;
3406 + const subagentUuid = "00000000-0000-4000-8000-000000000644";
3407 + yield* harness.runtime.startTurn(
3408 + makeClaudeTestTurnInput({
3409 + threadId: harness.threadId,
3410 + providerThread: harness.providerThread,
3411 + now,
3412 + attemptId: RunAttemptId.make("attempt-claude-cursor-subagent-only"),
3413 + text: "Background task completed.",
3414 + attachments: [],
3415 + }),
3416 + );
3417 + yield* Queue.offerAll(harness.sdkMessages, [
3418 + makeAssistantErrorFrame({
3419 + uuid: subagentUuid,
3420 + error: undefined,
3421 + parentToolUseId: "toolu_subagent",
3422 + }),
3423 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000645", result: "Done." }),
3424 + ]);
3425 + yield* Queue.take(harness.terminalReceipts);
3426 +
3427 + const providerTurn = lastProviderTurn(harness.events);
3428 + assert.notEqual(providerTurn.nativeTurnRef?.nativeId, subagentUuid);
3429 + const rollbackTo = (providerThreadTurns: ReadonlyArray<typeof providerTurn>) =>
3430 + harness.runtime.rollbackThread({
3431 + providerThread: harness.providerThread,
3432 + target: {
3433 + type: "provider_turn",
3434 + checkpointId: CheckpointId.make("checkpoint-claude-cursor-subagent-only"),
3435 + appRunOrdinal: 1,
3436 + providerTurn,
3437 + },
3438 + providerThreadTurns,
3439 + });
3440 +
3441 + const latest = yield* rollbackTo([providerTurn]);
3442 + assert.isNull(latest.providerThread.nativeConversationHeadRef);
3443 + const error = yield* rollbackTo([
3444 + providerTurn,
3445 + {
3446 + ...providerTurn,
3447 + id: ProviderTurnId.make("provider-turn-claude-cursor-next"),
3448 + ordinal: 2,
3449 + },
3450 + ]).pipe(Effect.flip);
3451 + assert.instanceOf(error, ProviderAdapter.ProviderAdapterRollbackThreadError);
3452 + assert.include(String(error.cause), "no SDK assistant message cursor");
3453 +
3454 + yield* harness.runtime.startTurn(
3455 + makeClaudeTestTurnInput({
3456 + threadId: harness.threadId,
3457 + providerThread: latest.providerThread,
3458 + now,
3459 + attemptId: RunAttemptId.make("attempt-claude-cursor-subagent-only-next"),
3460 + text: "Continue.",
3461 + attachments: [],
3462 + providerTurnOrdinal: 2,
3463 + }),
3464 + );
3465 + const reopenedQueue = harness.processQueues[1];
3466 + assert.isDefined(reopenedQueue);
3467 + if (reopenedQueue === undefined) return;
3468 + yield* Queue.offerAll(reopenedQueue, [
3469 + makeAssistantTextFrame({
3470 + uuid: "00000000-0000-4000-8000-000000000646",
3471 + text: "Continuing.",
3472 + }),
3473 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000647", result: "Done." }),
3474 + ]);
3475 + const next = yield* Queue.take(harness.terminalReceipts);
3476 + assert.equal(next.status, "completed");
3477 + // The reopened query must resume from the whole transcript, not pin a subagent uuid.
3478 + assert.isUndefined(harness.getOpenedOptions()?.resumeSessionAt);
3479 + }).pipe(
3480 + Effect.provide(
3481 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3482 + ),
3483 + ),
3484 + );
3485 +
3486 it.effect.each([
3487 { recovered: false, expected: "Claude usage limit reached" },
3488 { recovered: true, expected: "Claude gave up after repeated API errors" },
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+2 -1
@@ -7001,8 +7001,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7001 }
7002
7003 if (message.type === "assistant" && input.replayed !== true) {
7004 - context.nativeMessageCursor = message.uuid;
7004 if (message.parent_tool_use_id === null) {
7005 + // Subagent uuids exist only in the sidechain transcript, so resume and fork cannot target them.
7006 + context.nativeMessageCursor = message.uuid;
7007 context.latestAssistantRateLimited = message.error === "rate_limit";
7008 if (message.error === "authentication_failed") {
7009 context.authenticationFailureMessage = claudeSignedOutMessage({