@devries-cameron20 / t3code Mirror · read-only / commits / bfec2754b8
Fork 0

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, 21:00 UTC

fix(server): restart recovery wakes delegated children waiting on background work (#34833)

Julius Marminge committed Nov 11, 2026 at 23:26 UTC bfec2754b8bc4a6451cfd21cb128545ab138b795
2 files changed +111
apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
+108
@@ -1440,6 +1440,114 @@ it.effect("wakes a settled run whose native subagent ran on its own provider thr
1440 }).pipe(Effect.provide(layerRecoveryTest)),
1441 );
1442
1443 +it.effect("leaves a finished delegated child's background roster to the child", () =>
1444 + Effect.gen(function* () {
1445 + const projections = yield* ProjectionStore.ProjectionStoreV2;
1446 + const recovery = yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService;
1447 + const sql = yield* SqlClient.SqlClient;
1448 + const now = yield* DateTime.now;
1449 + // The parent sorts first, so it is reconciled before the child.
1450 + const parent = yield* createThread("delegation");
1451 + yield* createRun(parent, "completed");
1452 + const child = yield* createThread("delegation:child", {
1453 + lineage: { parentThreadId: parent, relationshipToParent: "subagent", rootThreadId: parent },
1454 + });
1455 + const childProviderThreadId = ProviderThreadId.make(`provider-thread:${child}`);
1456 + const childSessionId = ProviderSessionId.make(`session:${child}`);
1457 + yield* projections.apply({
1458 + id: EventId.make(`event:${child}:session`),
1459 + type: "provider-session.attached",
1460 + threadId: child,
1461 + driver,
1462 + providerInstanceId,
1463 + occurredAt: now,
1464 + payload: {
1465 + id: childSessionId,
1466 + driver,
1467 + providerInstanceId,
1468 + status: "ready",
1469 + cwd: "/workspace",
1470 + model: modelSelection.model,
1471 + capabilities: CodexProviderCapabilitiesV2,
1472 + createdAt: now,
1473 + updatedAt: now,
1474 + lastError: null,
1475 + },
1476 + });
1477 + const childRun = yield* createRun(child, "completed", {
1478 + providerThreadId: childProviderThreadId,
1479 + });
1480 + yield* projections.apply({
1481 + id: EventId.make(`event:${child}:roster`),
1482 + type: "provider-thread.updated",
1483 + threadId: child,
1484 + driver,
1485 + providerInstanceId,
1486 + occurredAt: now,
1487 + payload: {
1488 + id: childProviderThreadId,
1489 + appThreadId: child,
1490 + ownerNodeId: null,
1491 + driver,
1492 + providerInstanceId,
1493 + providerSessionId: childSessionId,
1494 + nativeThreadRef: { driver, nativeId: "child-thread", strength: "strong" },
1495 + nativeConversationHeadRef: null,
1496 + status: "idle",
1497 + firstRunOrdinal: 1,
1498 + lastRunOrdinal: 1,
1499 + handoffIds: [],
1500 + forkedFrom: null,
1501 + createdAt: now,
1502 + updatedAt: now,
1503 + pendingBackgroundTasks: [
1504 + { taskId: "wait", description: "sleep 600", kind: "command", awaited: true },
1505 + ],
1506 + },
1507 + });
1508 + // Finalizing a delegated task records the child's provider thread on the parent's row.
1509 + const taskId = NodeId.make(`node:${parent}:task`);
1510 + yield* projections.apply({
1511 + id: EventId.make(`event:${parent}:task`),
1512 + type: "subagent.updated",
1513 + threadId: parent,
1514 + occurredAt: now,
1515 + payload: {
1516 + id: taskId,
1517 + threadId: parent,
1518 + runId: RunId.make(`run:${parent}:1`),
1519 + parentNodeId: NodeId.make(`node:${parent}:root`),
1520 + origin: "app_owned",
1521 + createdBy: "agent",
1522 + driver,
1523 + providerInstanceId,
1524 + providerThreadId: childProviderThreadId,
1525 + childThreadId: child,
1526 + nativeTaskRef: null,
1527 + prompt: "wait for the build",
1528 + title: null,
1529 + model: null,
1530 + status: "completed",
1531 + result: "Waiting on the build",
1532 + startedAt: now,
1533 + completedAt: now,
1534 + updatedAt: now,
1535 + },
1536 + });
1537 +
1538 + yield* recovery.reconcile("startup");
1539 +
1540 + const effects = yield* sql<{ readonly effect_id: string }>`
1541 + SELECT effect_id FROM orchestration_v2_effect_outbox WHERE thread_id = ${child}
1542 + `;
1543 + assert.deepEqual(effects, [{ effect_id: `effect:restart-continuation:${childRun}` }]);
1544 + const projection = yield* projections.getThreadProjection(child);
1545 + assert.deepEqual(projection.runs[0]?.restartCancelledBackgroundWork, [
1546 + { kind: "shell", label: "sleep 600 (id wait)", id: "wait", awaited: true },
1547 + ]);
1548 + }).pipe(Effect.provide(layerRecoveryTest)),
1549 +);
1550 +
1551 it.effect("marks fork descendants unreadable when their source is missing or corrupt", () =>
1552 Effect.gen(function* () {
1553 const projections = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+3
@@ -4512,6 +4512,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4512 SELECT subagents.thread_id FROM pending_provider_threads
4513 CROSS JOIN orchestration_v2_projection_subagents AS subagents
4514 ON subagents.provider_thread_id = pending_provider_threads.provider_thread_id
4515 + -- A delegated task's provider thread belongs to its child thread.
4516 + WHERE subagents.origin = 'provider_native'
4517 UNION
4518 SELECT subagents.child_thread_id FROM orchestration_v2_projection_subagents AS subagents
4519 WHERE subagents.child_thread_id IS NOT NULL
@@ -5029,6 +5031,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5031 UNION
5032 SELECT provider_thread_id FROM orchestration_v2_projection_subagents
5033 WHERE thread_id = ${threadId} AND provider_thread_id IS NOT NULL
5034 + AND origin = 'provider_native'
5035 )
5036 AND (
5037 provider_thread.status = 'active'