Skip to content

Commit 36d0dcb

Browse files
StiensWoutcodexMuse Wonder Full 1.0claude
authored
fix(server): wake idle parents for late delegated results (pingdotgg#32573)
Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Muse Wonder Full 1.0 <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b76a595 commit 36d0dcb

6 files changed

Lines changed: 281 additions & 145 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,11 @@ const make = Effect.gen(function* () {
16551655
task.completionDelivery?.state !== "acknowledged" &&
16561656
task.completionDelivery?.state !== "disposed"
16571657
) {
1658-
const observingRun = ThreadManagementService.latestActiveRun(parentProjection);
1658+
const observingRun = ThreadManagementService.latestActiveRun({
1659+
runs: parentProjection.runs.filter(
1660+
(run) => run.providerInstanceId === scope.thread.providerInstanceId,
1661+
),
1662+
});
16591663
const acknowledgementRequestKey = yield* requestKey(undefined);
16601664
yield* threadManagement
16611665
.dispatch({
@@ -1667,10 +1671,7 @@ const make = Effect.gen(function* () {
16671671
}),
16681672
parentThreadId: scope.thread.threadId,
16691673
taskId,
1670-
observedByRunId:
1671-
observingRun?.providerInstanceId === scope.thread.providerInstanceId
1672-
? observingRun.id
1673-
: null,
1674+
observedByRunId: observingRun?.id ?? null,
16741675
})
16751676
.pipe(
16761677
Effect.mapError((error) =>
@@ -1750,7 +1751,11 @@ const make = Effect.gen(function* () {
17501751
),
17511752
),
17521753
);
1753-
const observingRun = ThreadManagementService.latestActiveRun(parentProjection);
1754+
const observingRun = ThreadManagementService.latestActiveRun({
1755+
runs: parentProjection.runs.filter(
1756+
(run) => run.providerInstanceId === scope.thread.providerInstanceId,
1757+
),
1758+
});
17541759
yield* threadManagement
17551760
.dispatch({
17561761
type: "delegated_task.completion-delivery.acknowledge",
@@ -1761,10 +1766,7 @@ const make = Effect.gen(function* () {
17611766
}),
17621767
parentThreadId: scope.thread.threadId,
17631768
taskId: task.id,
1764-
observedByRunId:
1765-
observingRun?.providerInstanceId === scope.thread.providerInstanceId
1766-
? observingRun.id
1767-
: null,
1769+
observedByRunId: observingRun?.id ?? null,
17681770
})
17691771
.pipe(
17701772
Effect.mapError((error) =>

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 143 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,73 @@ it.layer(layerContinuationWorkerTest)("interrupted completion wakes", (it) => {
16281628
it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
16291629
"subagent follow-ups",
16301630
(it) => {
1631+
it.effect("a late unobserved read wakes a pending result without restarting", () =>
1632+
Effect.gen(function* () {
1633+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1634+
const sink = yield* EventSink.EventSinkV2;
1635+
const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
1636+
const now = yield* DateTime.now;
1637+
const threadId = ThreadId.make("thread:late-read-live");
1638+
const runId = RunId.make("run:late-read-live");
1639+
const taskId = NodeId.make("task:late-read-live");
1640+
yield* seedParentWithTerminalTask({
1641+
threadId,
1642+
projectId: ProjectId.make("project:late-read-live"),
1643+
runId,
1644+
rootNodeId: NodeId.make("root:late-read-live"),
1645+
taskId,
1646+
deliveryState: "pending",
1647+
now,
1648+
});
1649+
const acknowledge = (key: string) =>
1650+
orchestrator.dispatch({
1651+
type: "delegated_task.completion-delivery.acknowledge",
1652+
commandId: CommandId.make(`command:late-read-live:${key}`),
1653+
parentThreadId: threadId,
1654+
taskId,
1655+
observedByRunId: null,
1656+
});
1657+
const before = yield* orchestrator.getThreadProjection(threadId);
1658+
const liveRead = yield* acknowledge("live");
1659+
assert.lengthOf(liveRead.storedEvents, 0);
1660+
const parent = before.runs.find((run) => run.id === runId)!;
1661+
yield* sink.write({
1662+
events: [
1663+
{
1664+
id: EventId.make("event:late-read-live:parent-ended"),
1665+
type: "run.updated",
1666+
threadId,
1667+
runId,
1668+
occurredAt: now,
1669+
payload: { ...parent, status: "completed", completedAt: now },
1670+
},
1671+
],
1672+
});
1673+
yield* acknowledge("ended");
1674+
const wake = yield* requests.take;
1675+
assert.equal(wake.threadId, threadId);
1676+
assert.equal(wake.delegatedCompletion?.parentRunId, runId);
1677+
const after = yield* orchestrator.getThreadProjection(threadId);
1678+
assert.equal(
1679+
after.subagents.find((task) => task.id === taskId)?.completionDelivery?.state,
1680+
"claimed",
1681+
);
1682+
assert.deepEqual(
1683+
after.runs.find((run) => run.id === runId)?.delegatedCompletion?.delivery?.taskIds,
1684+
[taskId],
1685+
);
1686+
const repeated = yield* acknowledge("repeated");
1687+
assert.lengthOf(repeated.storedEvents, 0);
1688+
assert.deepEqual(yield* orchestrator.getThreadProjection(threadId), after);
1689+
yield* orchestrator.dispatch({
1690+
type: "delegated_task.completion-delivery.dispose",
1691+
commandId: CommandId.make("command:late-read-live:dispose"),
1692+
parentThreadId: threadId,
1693+
taskId,
1694+
});
1695+
}),
1696+
);
1697+
16311698
it.effect("recovers a reserved delivery without loading conversation history", () =>
16321699
Effect.gen(function* () {
16331700
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -3418,70 +3485,84 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
34183485
}),
34193486
);
34203487

3421-
it.effect("wakes the parent for a settled_only task once its spawning turn ended", () =>
3422-
Effect.gen(function* () {
3423-
const orchestrator = yield* Orchestrator.OrchestratorV2;
3424-
const eventSink = yield* EventSink.EventSinkV2;
3425-
const now = yield* DateTime.now;
3426-
const threadId = ThreadId.make("thread:settled-only-restart");
3427-
const projectId = ProjectId.make("project:settled-only-restart");
3428-
const runId = RunId.make("run:settled-only-restart");
3429-
const rootNodeId = NodeId.make("node:settled-only-restart-root");
3430-
yield* seedParentWithTerminalTask({
3431-
threadId,
3432-
projectId,
3433-
runId,
3434-
rootNodeId,
3435-
taskId: NodeId.make("node:settled-only-restart-settled"),
3436-
deliveryState: "delivered",
3437-
now,
3438-
});
3439-
yield* eventSink.write({
3440-
commandId: reconcileCommandId("settled-only-restart"),
3441-
events: [
3442-
runEvent({
3443-
threadId,
3444-
runId,
3445-
ordinal: 1,
3446-
status: "cancelled",
3447-
now,
3448-
providerThreadId: parentProviderThreadId(threadId),
3449-
userMessageId: MessageId.make(`message:seed-user:${threadId}`),
3450-
delegatedCompletion: { disposition: "open", nextGeneration: 2, delivery: null },
3451-
}),
3452-
runEvent({
3453-
threadId,
3454-
runId: RunId.make("run:settled-only-restart:continuation"),
3455-
ordinal: 2,
3456-
status: "running",
3457-
now,
3458-
providerThreadId: parentProviderThreadId(threadId),
3459-
}),
3460-
],
3461-
});
3462-
const child = yield* seedRestartCancelledChild({
3463-
parentThreadId: threadId,
3464-
projectId,
3465-
parentRunId: runId,
3466-
rootNodeId,
3467-
name: "settled-only-restart-child",
3468-
completionWake: "settled_only",
3469-
continuationPending: false,
3470-
now,
3471-
});
3488+
it.effect.each([false, true])(
3489+
"wakes the parent for a settled_only result with a running continuation: %s",
3490+
(runningContinuation) =>
3491+
Effect.gen(function* () {
3492+
const orchestrator = yield* Orchestrator.OrchestratorV2;
3493+
const eventSink = yield* EventSink.EventSinkV2;
3494+
const now = yield* DateTime.now;
3495+
const suffix = runningContinuation ? "running" : "idle";
3496+
const threadId = ThreadId.make(`thread:settled-only-restart-${suffix}`);
3497+
const projectId = ProjectId.make(`project:settled-only-restart-${suffix}`);
3498+
const runId = RunId.make(`run:settled-only-restart-${suffix}`);
3499+
const rootNodeId = NodeId.make(`node:settled-only-restart-root-${suffix}`);
3500+
yield* seedParentWithTerminalTask({
3501+
threadId,
3502+
projectId,
3503+
runId,
3504+
rootNodeId,
3505+
taskId: NodeId.make(`node:settled-only-restart-settled-${suffix}`),
3506+
deliveryState: "delivered",
3507+
now,
3508+
});
3509+
yield* eventSink.write({
3510+
commandId: reconcileCommandId(`settled-only-restart-${suffix}`),
3511+
events: [
3512+
runEvent({
3513+
threadId,
3514+
runId,
3515+
ordinal: 1,
3516+
status: "cancelled",
3517+
now,
3518+
providerThreadId: parentProviderThreadId(threadId),
3519+
userMessageId: MessageId.make(`message:seed-user:${threadId}`),
3520+
delegatedCompletion: { disposition: "open", nextGeneration: 2, delivery: null },
3521+
}),
3522+
...(runningContinuation
3523+
? [
3524+
runEvent({
3525+
threadId,
3526+
runId: RunId.make(`run:settled-only-restart:continuation-${suffix}`),
3527+
ordinal: 2,
3528+
status: "running",
3529+
now,
3530+
providerThreadId: parentProviderThreadId(threadId),
3531+
}),
3532+
]
3533+
: []),
3534+
],
3535+
});
3536+
const child = yield* seedRestartCancelledChild({
3537+
parentThreadId: threadId,
3538+
projectId,
3539+
parentRunId: runId,
3540+
rootNodeId,
3541+
name: `settled-only-restart-child-${suffix}`,
3542+
completionWake: "settled_only",
3543+
continuationPending: false,
3544+
now,
3545+
});
34723546

3473-
yield* orchestrator.recoverDelegatedTasks;
3547+
yield* orchestrator.dispatch({
3548+
type: "delegated_task.completion-delivery.acknowledge",
3549+
commandId: CommandId.make(`command:settled-only-late-read-${suffix}`),
3550+
parentThreadId: threadId,
3551+
taskId: child.taskId,
3552+
observedByRunId: null,
3553+
});
3554+
yield* orchestrator.recoverDelegatedTasks;
34743555

3475-
const projection = yield* orchestrator.getThreadProjection(threadId);
3476-
assert.equal(
3477-
projection.subagents.find((row) => row.id === child.taskId)?.completionDelivery?.state,
3478-
"claimed",
3479-
);
3480-
assert.deepEqual(
3481-
projection.runs.find((row) => row.id === runId)?.delegatedCompletion?.delivery?.taskIds,
3482-
[child.taskId],
3483-
);
3484-
}),
3556+
const projection = yield* orchestrator.getThreadProjection(threadId);
3557+
assert.equal(
3558+
projection.subagents.find((row) => row.id === child.taskId)?.completionDelivery?.state,
3559+
"claimed",
3560+
);
3561+
assert.deepEqual(
3562+
projection.runs.find((row) => row.id === runId)?.delegatedCompletion?.delivery?.taskIds,
3563+
[child.taskId],
3564+
);
3565+
}),
34853566
);
34863567

34873568
it.effect.each(["a background command", "a nested subagent"] as const)(

0 commit comments

Comments
 (0)