Skip to content

Commit 78ff47c

Browse files
committed
fix(server): deliver pr wakes queued before the first child result
1 parent 2e9ef44 commit 78ff47c

2 files changed

Lines changed: 74 additions & 16 deletions

File tree

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

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,15 +1610,20 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
16101610
}),
16111611
);
16121612

1613-
it.effect.each(["a background command", "a pull request watch"] as const)(
1614-
"delivers the run %s wake starts after the original result",
1615-
(source) =>
1613+
it.effect.each([
1614+
["a background command", "after result"],
1615+
["a pull request watch", "after result"],
1616+
["a pull request watch", "before result"],
1617+
["a pull request watch", "while running"],
1618+
] as const)(
1619+
"delivers the result of the run %s wake starts, with the original task %s",
1620+
([source, timing]) =>
16161621
Effect.gen(function* () {
16171622
const orchestrator = yield* Orchestrator.OrchestratorV2;
16181623
const eventSink = yield* EventSink.EventSinkV2;
16191624
const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
16201625
const now = yield* DateTime.now;
1621-
const key = source === "a background command" ? "background-wake" : "pr-watch-wake";
1626+
const key = `${source}-${timing}`;
16221627
const parentThreadId = ThreadId.make(`thread:${key}-parent`);
16231628
const projectId = ProjectId.make(`project:${key}`);
16241629
const parentRunId = RunId.make(`run:${key}-parent`);
@@ -1643,16 +1648,38 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
16431648
runStatus: "completed",
16441649
now,
16451650
});
1646-
yield* orchestrator.recoverDelegatedTasks;
1647-
let firstWake = yield* requests.take;
1648-
while (firstWake.threadId !== parentThreadId) firstWake = yield* requests.take;
1649-
yield* orchestrator.dispatch({
1650-
type: "delegated_task.completion-delivery.acknowledge",
1651-
commandId: CommandId.make(`command:${key}-ack`),
1652-
parentThreadId,
1653-
taskId: child.taskId,
1654-
observedByRunId: parentRunId,
1655-
});
1651+
let activeRun: OrchestrationV2Run | undefined;
1652+
if (timing === "while running") {
1653+
const messageId = MessageId.make(`message:${key}-active`);
1654+
yield* orchestrator.dispatch({
1655+
type: "message.dispatch",
1656+
commandId: CommandId.make(`command:${key}-active`),
1657+
threadId: child.childThreadId,
1658+
messageId,
1659+
text: "Keep working on the original task.",
1660+
attachments: [],
1661+
dispatchMode: { type: "start_immediately" },
1662+
createdBy: "agent",
1663+
creationSource: "mcp",
1664+
});
1665+
activeRun = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs.find(
1666+
(row) => row.userMessageId === messageId,
1667+
)!;
1668+
}
1669+
let firstWakeMessageId: MessageId | undefined;
1670+
if (timing === "after result") {
1671+
yield* orchestrator.recoverDelegatedTasks;
1672+
let firstWake = yield* requests.take;
1673+
while (firstWake.threadId !== parentThreadId) firstWake = yield* requests.take;
1674+
yield* orchestrator.dispatch({
1675+
type: "delegated_task.completion-delivery.acknowledge",
1676+
commandId: CommandId.make(`command:${key}-ack`),
1677+
parentThreadId,
1678+
taskId: child.taskId,
1679+
observedByRunId: parentRunId,
1680+
});
1681+
firstWakeMessageId = firstWake.delegatedCompletion?.messageId;
1682+
}
16561683
const parentRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs.find(
16571684
(run) => run.id === parentRunId,
16581685
)!;
@@ -1728,6 +1755,32 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
17281755
(row) => row.userMessageId === wakeMessageId,
17291756
)!;
17301757
assert.isDefined(run.delegatedTaskId);
1758+
if (timing === "while running") {
1759+
assert.equal(run.status, "queued");
1760+
yield* eventSink.write({
1761+
commandId: reconcileCommandId(`${key}-initial-finished`),
1762+
events: [
1763+
runEvent({
1764+
threadId: child.childThreadId,
1765+
runId: activeRun!.id,
1766+
ordinal: activeRun!.ordinal,
1767+
status: "completed",
1768+
now,
1769+
}),
1770+
],
1771+
});
1772+
yield* orchestrator.dispatch({
1773+
type: "queue.resume",
1774+
commandId: CommandId.make(`command:${key}-resume`),
1775+
threadId: child.childThreadId,
1776+
});
1777+
assert.notEqual(
1778+
(yield* orchestrator.getThreadProjection(child.childThreadId)).runs.find(
1779+
(row) => row.id === run.id,
1780+
)?.status,
1781+
"queued",
1782+
);
1783+
}
17311784
yield* eventSink.write({
17321785
commandId: reconcileCommandId(`${key}-finished`),
17331786
events: [
@@ -1778,7 +1831,7 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
17781831
let wake = yield* requests.take;
17791832
while (
17801833
wake.threadId !== parentThreadId ||
1781-
wake.delegatedCompletion?.messageId === firstWake.delegatedCompletion?.messageId
1834+
wake.delegatedCompletion?.messageId === firstWakeMessageId
17821835
) {
17831836
wake = yield* requests.take;
17841837
}

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7655,7 +7655,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
76557655
task.childThreadId === child.id &&
76567656
task.id === (child.forkedFrom?.type === "node" ? child.forkedFrom.nodeId : undefined),
76577657
);
7658-
if (original === undefined || original.result === null) return;
7658+
// PR watch wakes can queue before the original task publishes its result.
7659+
if (
7660+
original === undefined ||
7661+
(original.result === null && command.notification?.source.kind !== "monitor")
7662+
)
7663+
return;
76597664
const owner =
76607665
parent.runs.filter(isBlockingRun).toSorted((a, b) => b.ordinal - a.ordinal)[0] ??
76617666
latestExecutedRun(parent.runs);

0 commit comments

Comments
 (0)