The best way to control Claude Code, Codex and more from any computer or phone
fix(server): notify parents after delegated background runs (#23542)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 25, 2026 at 13:00 UTC
0504c99205e427ed1a6a571b2d4c4ece910a8a65
2 files changed
+153
-12
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+132
@@ -1404,6 +1404,138 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
1404
}),
1405
);
1406
1407
+ it.effect("delivers the run a background command's wake starts after the original result", () =>
1408
+ Effect.gen(function* () {
1409
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1410
+ const eventSink = yield* EventSink.EventSinkV2;
1411
+ const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
1412
+ const now = yield* DateTime.now;
1413
+ const parentThreadId = ThreadId.make("thread:background-wake-parent");
1414
+ const projectId = ProjectId.make("project:background-wake");
1415
+ const parentRunId = RunId.make("run:background-wake-parent");
1416
+ const rootNodeId = NodeId.make("node:background-wake-parent");
1417
+ yield* seedParentWithTerminalTask({
1418
+ threadId: parentThreadId,
1419
+ projectId,
1420
+ runId: parentRunId,
1421
+ rootNodeId,
1422
+ taskId: NodeId.make("node:background-wake-seed"),
1423
+ deliveryState: "acknowledged",
1424
+ now,
1425
+ });
1426
+ const child = yield* seedRestartCancelledChild({
1427
+ parentThreadId,
1428
+ projectId,
1429
+ parentRunId,
1430
+ rootNodeId,
1431
+ name: "background-wake-child",
1432
+ completionWake: "always",
1433
+ continuationPending: false,
1434
+ runStatus: "completed",
1435
+ now,
1436
+ });
1437
+ yield* orchestrator.recoverDelegatedTasks;
1438
+ let firstWake = yield* requests.take;
1439
+ while (firstWake.threadId !== parentThreadId) firstWake = yield* requests.take;
1440
+ yield* orchestrator.dispatch({
1441
+ type: "delegated_task.completion-delivery.acknowledge",
1442
+ commandId: CommandId.make("command:background-wake-ack"),
1443
+ parentThreadId,
1444
+ taskId: child.taskId,
1445
+ observedByRunId: parentRunId,
1446
+ });
1447
+ const parentRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs.find(
1448
+ (run) => run.id === parentRunId,
1449
+ )!;
1450
+ yield* eventSink.write({
1451
+ commandId: reconcileCommandId("background-wake-parent-finished"),
1452
+ events: [
1453
+ {
1454
+ id: EventId.make("event:background-wake-parent-finished"),
1455
+ type: "run.updated",
1456
+ threadId: parentThreadId,
1457
+ runId: parentRunId,
1458
+ occurredAt: now,
1459
+ payload: { ...parentRun, status: "completed", completedAt: now },
1460
+ },
1461
+ ],
1462
+ });
1463
+
1464
+ // The shape ProviderContinuationService dispatches when the provider
1465
+ // reports a background command (wire kind `background_command`) ended.
1466
+ const wakeMessageId = MessageId.make("message:background-wake");
1467
+ yield* orchestrator.dispatch({
1468
+ type: "message.dispatch",
1469
+ createdBy: "agent",
1470
+ creationSource: "provider",
1471
+ commandId: CommandId.make("command:background-wake"),
1472
+ threadId: child.childThreadId,
1473
+ messageId: wakeMessageId,
1474
+ text: "Background activity updated",
1475
+ notification: {
1476
+ source: { kind: "command" },
1477
+ outcome: "completed",
1478
+ summary: 'Command "sleep 60" finished',
1479
+ },
1480
+ attachments: [],
1481
+ dispatchMode: { type: "queue_after_active" },
1482
+ });
1483
+ const run = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs.find(
1484
+ (row) => row.userMessageId === wakeMessageId,
1485
+ )!;
1486
+ assert.isDefined(run.delegatedTaskId);
1487
+ yield* eventSink.write({
1488
+ commandId: reconcileCommandId("background-wake-finished"),
1489
+ events: [
1490
+ {
1491
+ id: EventId.make("event:background-wake-result"),
1492
+ type: "message.updated",
1493
+ threadId: child.childThreadId,
1494
+ runId: run.id,
1495
+ occurredAt: now,
1496
+ payload: {
1497
+ id: MessageId.make("message:background-wake-result"),
1498
+ threadId: child.childThreadId,
1499
+ runId: run.id,
1500
+ nodeId: null,
1501
+ role: "assistant",
1502
+ text: "The background command finished cleanly.",
1503
+ attachments: [],
1504
+ streaming: false,
1505
+ createdBy: "agent",
1506
+ creationSource: "server",
1507
+ createdAt: now,
1508
+ updatedAt: now,
1509
+ },
1510
+ },
1511
+ {
1512
+ id: EventId.make("event:background-wake-finished"),
1513
+ type: "run.updated",
1514
+ threadId: child.childThreadId,
1515
+ runId: run.id,
1516
+ occurredAt: now,
1517
+ payload: { ...run, status: "completed", startedAt: now, completedAt: now },
1518
+ },
1519
+ ],
1520
+ });
1521
+ yield* orchestrator.recoverDelegatedTasks;
1522
+ const followup = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
1523
+ (task) => task.id === run.delegatedTaskId,
1524
+ )!;
1525
+ assert.equal(followup.status, "completed");
1526
+ assert.equal(followup.result, "The background command finished cleanly.");
1527
+ assert.equal(followup.completionDelivery?.state, "claimed");
1528
+ let wake = yield* requests.take;
1529
+ while (
1530
+ wake.threadId !== parentThreadId ||
1531
+ wake.delegatedCompletion?.messageId === firstWake.delegatedCompletion?.messageId
1532
+ ) {
1533
+ wake = yield* requests.take;
1534
+ }
1535
+ assert.equal(wake.delegatedCompletion?.parentRunId, parentRunId);
1536
+ }),
1537
+ );
1538
+
1539
const outcomes = [
1540
"completed",
1541
"failed",
apps/server/src/orchestration-v2/Orchestrator.ts
+21
-12
@@ -600,6 +600,22 @@ function cancelledAppOwnedTask(
600
};
601
}
602
603
+/**
604
+ * Whether a message sent into a delegated child may become a follow-up task the parent hears
605
+ * back from. Besides a person or agent writing to the child, a provider wake counts, such as a
606
+ * background command finishing: the child acts on it after its original result was delivered.
607
+ * A monitor update does not, since it reports progress rather than asking for new work.
608
+ * Dispatch takes the parent's lock for exactly these, because intake writes the parent.
609
+ */
610
+function mayStartSubagentFollowup(
611
+ command: Extract<OrchestrationV2Command, { readonly type: "message.dispatch" }>,
612
+): boolean {
613
+ if (command.scheduledTaskId !== undefined || command.restartContinuationOfRunId !== undefined)
614
+ return false;
615
+ if (command.notification !== undefined) return command.notification.source.kind !== "monitor";
616
+ return command.createdBy === "user" || command.creationSource === "mcp";
617
+}
618
+
619
function isBlockingRun(run: OrchestrationV2Run): boolean {
620
return (
621
run.status === "preparing" ||
@@ -7313,13 +7329,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7329
command: Extract<OrchestrationV2Command, { readonly type: "message.dispatch" }>,
7330
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
7331
) {
7316
- if (
7317
- command.scheduledTaskId !== undefined ||
7318
- command.notification !== undefined ||
7319
- command.restartContinuationOfRunId !== undefined ||
7320
- (command.createdBy !== "user" && command.creationSource !== "mcp")
7321
- )
7322
- return;
7332
+ if (!mayStartSubagentFollowup(command)) return;
7333
const pending = yield* Ref.get(events);
7334
const message = pending
7335
.flatMap((event) =>
@@ -7341,7 +7351,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7351
if (
7352
child.lineage.relationshipToParent !== "subagent" ||
7353
parentThreadId === null ||
7344
- (command.createdBy !== "user" && command.senderThreadId !== parentThreadId)
7354
+ (command.notification === undefined &&
7355
+ command.createdBy !== "user" &&
7356
+ command.senderThreadId !== parentThreadId)
7357
)
7358
return;
7359
const run =
@@ -12117,10 +12129,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12129
commandThreadId(command),
12130
dispatchWithReceiptEffect(command),
12131
);
12120
- if (
12121
- command.type !== "message.dispatch" ||
12122
- (command.createdBy !== "user" && command.creationSource !== "mcp")
12123
- )
12132
+ if (command.type !== "message.dispatch" || !mayStartSubagentFollowup(command))
12133
return yield* dispatch;
12134
const parentThreadId = yield* appOwnedSubagentParentThreadId(command.threadId).pipe(
12135
Effect.mapError(