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 21, 22:15 UTC
fix(server): forks whose source session is gone start fresh instead of failing every send (#27729)
Julius Marminge committed
Nov 11, 2026 at 15:26 UTC
c3e5bfc027176c17c7d16e0d758eed120656e7b9
2 files changed
+319
-11
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+76
-11
@@ -116,6 +116,18 @@ const refusedBeforePrompt = (error: unknown): boolean =>
116
PROMPT_REFUSAL_TAGS.some((tag) => Predicate.isTagged(error, tag)) ||
117
(Predicate.hasProperty(error, "cause") && refusedBeforePrompt(error.cause));
118
119
+const startFailureMessage = (error: Error): string => {
120
+ const nestedCause = "cause" in error ? error.cause : undefined;
121
+ return (
122
+ knownProviderFailureMessage(error) ??
123
+ (nestedCause instanceof Error
124
+ ? nestedCause.message
125
+ : typeof nestedCause === "string"
126
+ ? nestedCause
127
+ : error.message)
128
+ );
129
+};
130
+
131
export interface ProviderTurnStartServiceV2Shape {
132
readonly start: (input: {
133
readonly threadId: ThreadId;
@@ -525,7 +537,6 @@ export const layer: Layer.Layer<
537
readonly error: Error;
538
readonly message?: string;
539
}) {
528
- const nestedCause = "cause" in failed.error ? failed.error.cause : undefined;
540
yield* settleRunBeforeStart({
541
signal: failed.signal,
542
status: "failed",
@@ -537,14 +548,7 @@ export const layer: Layer.Layer<
548
title: failed.title,
549
failure: makeProviderFailure({
550
cause: failed.error,
540
- message:
541
- failed.message ??
542
- knownProviderFailureMessage(failed.error) ??
543
- (nestedCause instanceof Error
544
- ? nestedCause.message
545
- : typeof nestedCause === "string"
546
- ? nestedCause
547
- : failed.error.message),
551
+ message: failed.message ?? startFailureMessage(failed.error),
552
class: "provider_error",
553
}),
554
},
@@ -1151,6 +1155,7 @@ export const layer: Layer.Layer<
1155
const sourceBusy = sourceProjection.runs.some(
1156
(candidate) => candidate.ordinal > sourceRun.ordinal && isActiveRun(candidate),
1157
);
1158
+ let nativeForkFailure: string | undefined;
1159
if (!sourceBusy) {
1160
// Rolled-back turns are no longer in the source conversation, so they cannot bound a fork.
1161
const rolledBackAttemptIds = new Set(
@@ -1163,7 +1168,7 @@ export const layer: Layer.Layer<
1168
)
1169
.map((attempt) => attempt.id),
1170
);
1166
- return yield* loadFromProvider(
1171
+ const forked = yield* Effect.result(
1172
session.forkThread({
1173
sourceProviderThread,
1174
sourceProviderTurns: sourceProjection.providerTurns.filter(
@@ -1178,7 +1183,24 @@ export const layer: Layer.Layer<
1183
: { providerTurnId: sourceProviderTurn.id }),
1184
}),
1185
);
1186
+ if (forked._tag === "Success") return forked.success;
1187
+ if (input.willRetry === true) return yield* forked.failure;
1188
+ if (!(yield* isCurrentAttemptInStatus("starting"))) return undefined;
1189
+ // Retries are spent (e.g. the source's native session is gone): start fresh with portable history.
1190
+ yield* Effect.logWarning("Starting a native fork in a fresh native session", {
1191
+ driver: session.driver,
1192
+ providerThreadId: providerThread.id,
1193
+ sourceProviderThreadId: sourceProviderThread.id,
1194
+ runId,
1195
+ errorTag: forked.failure._tag,
1196
+ });
1197
+ nativeForkFailure = makeProviderFailure({
1198
+ cause: forked.failure,
1199
+ message: startFailureMessage(forked.failure),
1200
+ class: "provider_error",
1201
+ }).message;
1202
}
1203
+ const nativeForkFailed = nativeForkFailure !== undefined;
1204
const sourceItems = yield* ContextHandoffService.readForkHistoryItems(
1205
projectionStore,
1206
sourceProjection,
@@ -1209,7 +1231,11 @@ export const layer: Layer.Layer<
1231
modelSelection: run.modelSelection,
1232
runtimePolicy: resolvedRuntimePolicy,
1233
providerSessionId,
1212
- existingProviderThread: { ...providerThread, forkedFrom: null },
1234
+ existingProviderThread: nativeForkFailed
1235
+ ? { ...providerThread, nativeThreadRef: null, forkedFrom: null }
1236
+ : { ...providerThread, forkedFrom: null },
1237
+ // The failed fork may have left the adapter's session half-switched.
1238
+ ...(nativeForkFailed ? { forceNewNativeThread: true } : {}),
1239
}),
1240
);
1241
if (loaded === undefined) return undefined;
@@ -1242,6 +1268,45 @@ export const layer: Layer.Layer<
1268
updatedAt: createdAt,
1269
},
1270
},
1271
+ ...(nativeForkFailure === undefined
1272
+ ? []
1273
+ : [
1274
+ {
1275
+ id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }),
1276
+ type: "turn-item.updated" as const,
1277
+ threadId: projection.thread.id,
1278
+ runId: run.id,
1279
+ providerInstanceId: run.providerInstanceId,
1280
+ occurredAt: createdAt,
1281
+ payload: {
1282
+ id: idAllocator.derive.runSignalTurnItem({
1283
+ runId,
1284
+ signal: "native-fork-fallback",
1285
+ }),
1286
+ threadId: projection.thread.id,
1287
+ runId,
1288
+ nodeId: rootNode.id,
1289
+ providerThreadId: providerThread.id,
1290
+ providerTurnId: null,
1291
+ nativeItemRef: null,
1292
+ parentItemId: null,
1293
+ ordinal:
1294
+ Math.max(
1295
+ 0,
1296
+ ...projection.turnItems
1297
+ .filter((item) => item.runId === runId)
1298
+ .map((item) => item.ordinal),
1299
+ ) + 1,
1300
+ status: "completed" as const,
1301
+ title: "Native fork failed",
1302
+ startedAt: createdAt,
1303
+ completedAt: createdAt,
1304
+ updatedAt: createdAt,
1305
+ type: "system_notice" as const,
1306
+ message: `Could not fork the source conversation natively (${nativeForkFailure}). Started a fresh session with its history.`,
1307
+ },
1308
+ },
1309
+ ]),
1310
],
1311
});
1312
return loaded;
apps/server/src/orchestration-v2/ThreadFork.execution.test.ts
+243
@@ -18,8 +18,10 @@ import {
18
TurnItemId,
19
} from "@t3tools/contracts";
20
import * as DateTime from "effect/DateTime";
21
+import * as Deferred from "effect/Deferred";
22
import * as Effect from "effect/Effect";
23
import * as Statement from "effect/sql/Statement";
24
+import * as Stream from "effect/Stream";
25
import * as TestClock from "effect/testing/TestClock";
26
27
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -1311,3 +1313,244 @@ it.effect("merge-back rejects a second fork's context until the first is consume
1313
);
1314
}).pipe(Effect.provide(forkCases[0]!.layer)),
1315
);
1316
+
1317
+const runForkWithFailingNativeSource = (forkFailures: number) => {
1318
+ const driver = ProviderDriverKind.make("claudeAgent");
1319
+ const instanceId = ProviderInstanceId.make("claudeAgent");
1320
+ const modelSelection = { instanceId, model: "test-model" };
1321
+ const started = Deferred.makeUnsafe<ProviderAdapter.ProviderAdapterV2TurnInput>();
1322
+ let forkCalls = 0;
1323
+ const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
1324
+ instanceId,
1325
+ driver,
1326
+ getCapabilities: () => Effect.succeed(ClaudeProviderCapabilitiesV2),
1327
+ planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
1328
+ openSession: (input) =>
1329
+ Effect.gen(function* () {
1330
+ const now = yield* DateTime.now;
1331
+ return {
1332
+ instanceId,
1333
+ driver,
1334
+ providerSessionId: input.providerSessionId,
1335
+ providerSession: {
1336
+ id: input.providerSessionId,
1337
+ driver,
1338
+ providerInstanceId: instanceId,
1339
+ status: "ready",
1340
+ cwd: "/tmp/fork-missing-native-source",
1341
+ model: modelSelection.model,
1342
+ capabilities: ClaudeProviderCapabilitiesV2,
1343
+ createdAt: now,
1344
+ updatedAt: now,
1345
+ lastError: null,
1346
+ },
1347
+ events: Stream.never,
1348
+ ensureThread: ({ threadId, existingProviderThread }) =>
1349
+ Effect.succeed({
1350
+ ...existingProviderThread!,
1351
+ appThreadId: threadId,
1352
+ nativeThreadRef: { driver, nativeId: "native-fresh-fork", strength: "strong" },
1353
+ status: "idle",
1354
+ updatedAt: now,
1355
+ }),
1356
+ resumeThread: ({ providerThread }) => Effect.succeed(providerThread),
1357
+ startTurn: (turn) => Deferred.succeed(started, turn).pipe(Effect.asVoid),
1358
+ steerTurn: () => Effect.void,
1359
+ interruptTurn: () => Effect.void,
1360
+ respondToRuntimeRequest: () => Effect.void,
1361
+ readThreadSnapshot: () => Effect.die("unused"),
1362
+ rollbackThread: () => Effect.die("unused"),
1363
+ forkThread: ({ sourceProviderThread, targetThreadId }) =>
1364
+ ++forkCalls <= forkFailures
1365
+ ? Effect.fail(
1366
+ new ProviderAdapter.ProviderAdapterForkThreadError({
1367
+ driver,
1368
+ providerThreadId: sourceProviderThread.id,
1369
+ cause:
1370
+ "Session not found: native-deleted-source; authorization=BearerSyntheticSecret",
1371
+ }),
1372
+ )
1373
+ : Effect.succeed({
1374
+ ...sourceProviderThread,
1375
+ appThreadId: targetThreadId,
1376
+ nativeThreadRef: { driver, nativeId: "native-forked", strength: "strong" },
1377
+ updatedAt: now,
1378
+ }),
1379
+ } satisfies ProviderAdapter.ProviderAdapterV2SessionRuntime;
1380
+ }),
1381
+ };
1382
+ return Effect.gen(function* () {
1383
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1384
+ const eventSink = yield* EventSink.EventSinkV2;
1385
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1386
+ const now = yield* DateTime.now;
1387
+ const sourceThreadId = ThreadId.make("fork-missing-source");
1388
+ const targetThreadId = ThreadId.make("fork-missing-target");
1389
+ const providerThreadId = ProviderThreadId.make("fork-missing-native-thread");
1390
+ const sourceRunId = RunId.make("fork-missing-source-run");
1391
+ const userMessageId = MessageId.make("fork-missing-source-message");
1392
+
1393
+ yield* orchestrator.dispatch({
1394
+ type: "thread.create",
1395
+ commandId: CommandId.make("fork-missing-create-source"),
1396
+ threadId: sourceThreadId,
1397
+ projectId: ProjectId.make("fork-missing-project"),
1398
+ title: "Fork missing source",
1399
+ modelSelection,
1400
+ runtimeMode: "full-access",
1401
+ interactionMode: "default",
1402
+ branch: null,
1403
+ worktreePath: null,
1404
+ createdBy: "user",
1405
+ creationSource: "web",
1406
+ });
1407
+ yield* eventSink.write({
1408
+ events: [
1409
+ {
1410
+ id: EventId.make("fork-missing-provider-thread"),
1411
+ type: "provider-thread.updated",
1412
+ threadId: sourceThreadId,
1413
+ occurredAt: now,
1414
+ payload: {
1415
+ id: providerThreadId,
1416
+ driver,
1417
+ providerInstanceId: instanceId,
1418
+ providerSessionId: null,
1419
+ appThreadId: sourceThreadId,
1420
+ ownerNodeId: null,
1421
+ nativeThreadRef: { driver, nativeId: "native-deleted-source", strength: "strong" },
1422
+ nativeConversationHeadRef: null,
1423
+ status: "idle",
1424
+ firstRunOrdinal: 1,
1425
+ lastRunOrdinal: 1,
1426
+ handoffIds: [],
1427
+ forkedFrom: null,
1428
+ createdAt: now,
1429
+ updatedAt: now,
1430
+ },
1431
+ },
1432
+ {
1433
+ id: EventId.make("fork-missing-source-run"),
1434
+ type: "run.created",
1435
+ threadId: sourceThreadId,
1436
+ runId: sourceRunId,
1437
+ occurredAt: now,
1438
+ payload: {
1439
+ id: sourceRunId,
1440
+ threadId: sourceThreadId,
1441
+ ordinal: 1,
1442
+ providerInstanceId: instanceId,
1443
+ modelSelection,
1444
+ providerThreadId,
1445
+ userMessageId,
1446
+ rootNodeId: null,
1447
+ activeAttemptId: null,
1448
+ status: "completed",
1449
+ queuePosition: null,
1450
+ requestedAt: now,
1451
+ startedAt: now,
1452
+ completedAt: now,
1453
+ checkpointId: null,
1454
+ contextHandoffId: null,
1455
+ },
1456
+ },
1457
+ {
1458
+ id: EventId.make("fork-missing-user-item"),
1459
+ type: "turn-item.updated",
1460
+ threadId: sourceThreadId,
1461
+ runId: sourceRunId,
1462
+ occurredAt: now,
1463
+ payload: {
1464
+ id: TurnItemId.make("fork-missing-user-item"),
1465
+ threadId: sourceThreadId,
1466
+ runId: sourceRunId,
1467
+ nodeId: null,
1468
+ providerThreadId,
1469
+ providerTurnId: null,
1470
+ nativeItemRef: null,
1471
+ parentItemId: null,
1472
+ ordinal: 1,
1473
+ status: "completed",
1474
+ title: null,
1475
+ startedAt: now,
1476
+ completedAt: now,
1477
+ updatedAt: now,
1478
+ type: "user_message",
1479
+ createdBy: "user",
1480
+ creationSource: "web",
1481
+ inputIntent: "turn_start",
1482
+ messageId: userMessageId,
1483
+ text: "FORKED_SOURCE_MARKER",
1484
+ attachments: [],
1485
+ },
1486
+ },
1487
+ ],
1488
+ });
1489
+ yield* orchestrator.dispatch({
1490
+ type: "thread.fork",
1491
+ commandId: CommandId.make("fork-missing-fork"),
1492
+ sourceThreadId,
1493
+ targetThreadId,
1494
+ sourcePoint: { type: "run", runId: sourceRunId },
1495
+ createdBy: "user",
1496
+ creationSource: "web",
1497
+ });
1498
+ yield* orchestrator.dispatch({
1499
+ type: "message.dispatch",
1500
+ commandId: CommandId.make("fork-missing-continue"),
1501
+ threadId: targetThreadId,
1502
+ messageId: MessageId.make("fork-missing-continue"),
1503
+ text: "Continue the fork",
1504
+ attachments: [],
1505
+ modelSelection,
1506
+ dispatchMode: { type: "start_immediately" },
1507
+ createdBy: "user",
1508
+ creationSource: "web",
1509
+ });
1510
+ // Run every start attempt so the last one runs with willRetry false.
1511
+ for (let attempt = 0; attempt < 5; attempt++) {
1512
+ yield* worker.drain();
1513
+ yield* TestClock.adjust("1 second");
1514
+ }
1515
+
1516
+ const projection = yield* orchestrator.getThreadProjection(targetThreadId);
1517
+ assert.deepEqual(
1518
+ projection.turnItems.flatMap((item) => (item.type === "error" ? [item.title] : [])),
1519
+ [],
1520
+ );
1521
+ assert.equal(projection.runs[0]?.status, "running");
1522
+ return { projection, turn: yield* Deferred.await(started), forkCalls };
1523
+ }).pipe(
1524
+ Effect.provide(
1525
+ ProviderReplayHarness.layerWithRegistry(
1526
+ { name: "fork-missing-native-source" },
1527
+ ProviderAdapterRegistry.layerFromAdapters([adapter]),
1528
+ ),
1529
+ ),
1530
+ );
1531
+};
1532
+
1533
+it.effect("a native fork whose source session is gone starts fresh with the source history", () =>
1534
+ Effect.gen(function* () {
1535
+ const { projection, turn } = yield* runForkWithFailingNativeSource(Number.POSITIVE_INFINITY);
1536
+ // Resolving the fork transfer stops later sends from retrying the native fork.
1537
+ assert.equal(projection.contextTransfers[0]?.status, "resolved_portable");
1538
+ assert.isNull(projection.providerThreads[0]?.forkedFrom);
1539
+ assert.include(JSON.stringify(turn), "FORKED_SOURCE_MARKER");
1540
+ const notices = projection.turnItems.flatMap((item) =>
1541
+ item.type === "system_notice" ? [item.message] : [],
1542
+ );
1543
+ assert.lengthOf(notices, 1);
1544
+ assert.include(notices[0], "Session not found: native-deleted-source");
1545
+ assert.notInclude(notices[0], "BearerSyntheticSecret");
1546
+ }),
1547
+);
1548
+
1549
+it.effect("a native fork that fails before its last attempt still forks natively", () =>
1550
+ Effect.gen(function* () {
1551
+ const { projection, forkCalls } = yield* runForkWithFailingNativeSource(1);
1552
+ assert.equal(forkCalls, 2);
1553
+ assert.equal(projection.contextTransfers[0]?.status, "consumed");
1554
+ assert.equal(projection.contextTransfers[0]?.resolution?.strategy, "native_fork");
1555
+ }),
1556
+);