Skip to content

Commit 74912d7

Browse files
fix(server): preserve queued monitor continuation identity
1 parent 416c14c commit 74912d7

3 files changed

Lines changed: 161 additions & 43 deletions

File tree

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

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,21 @@ function nextQueuedRun(
617617
return queuedRunsInDeliveryOrder(projection)[0];
618618
}
619619

620+
function followsCurrentConversation(
621+
message: OrchestrationV2ConversationMessage | undefined,
622+
): boolean {
623+
return (
624+
message?.notification?.source.kind === "monitor" && !isBufferedProviderNotification(message)
625+
);
626+
}
627+
620628
function queuedRunModelSelection(
621629
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "messages">,
622630
run: OrchestrationV2Run,
623631
): ModelSelection {
624632
const message = projection.messages.find((candidate) => candidate.id === run.userMessageId);
625-
// Monitor updates follow the agent currently handling the thread.
626-
return message?.notification?.source.kind === "monitor"
633+
// App-owned monitor updates follow the agent currently handling the thread.
634+
return followsCurrentConversation(message)
627635
? projection.thread.modelSelection
628636
: run.modelSelection;
629637
}
@@ -1339,6 +1347,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13391347
modelSelection,
13401348
providerInstanceId: modelSelection.instanceId,
13411349
};
1350+
// A limited provider would fail and consume every waiting message.
13421351
if (usageLimitHoldsQueue(projection, queuedRun.providerInstanceId)) {
13431352
return;
13441353
}
@@ -1461,12 +1470,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14611470
const activeProviderThread = projection.providerThreads.find(
14621471
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
14631472
);
1464-
const targetProviderThread =
1465-
queuedMessage.notification?.source.kind === "monitor"
1466-
? activeProviderThread?.providerInstanceId === queuedRun.providerInstanceId
1467-
? activeProviderThread
1468-
: rootProviderThreadsForProvider(projection, queuedRun.providerInstanceId)[0]
1469-
: storedProviderThread;
1473+
const followsCurrent = followsCurrentConversation(queuedMessage);
1474+
const targetProviderThread = followsCurrent
1475+
? activeProviderThread?.providerInstanceId === queuedRun.providerInstanceId
1476+
? activeProviderThread
1477+
: rootProviderThreadsForProvider(projection, queuedRun.providerInstanceId)[0]
1478+
: storedProviderThread;
14701479
const queuedProviderThread: OrchestrationV2ProviderThread = targetProviderThread ?? {
14711480
id: idAllocator.derive.providerThread({
14721481
driver: targetAdapter.driver,
@@ -1491,20 +1500,40 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14911500
projection.thread.modelSelection,
14921501
queuedRun.modelSelection,
14931502
);
1494-
const switchPlan = selectionChanged
1495-
? yield* providerSwitchService
1496-
.plan({ projection, targetModelSelection: queuedRun.modelSelection })
1497-
.pipe(
1498-
Effect.mapError(
1499-
(cause) =>
1500-
new OrchestratorDispatchError({
1501-
commandId,
1502-
commandType: "message.dispatch",
1503-
cause,
1504-
}),
1505-
),
1506-
)
1507-
: null;
1503+
// The picker may already have moved ahead of the native conversation.
1504+
const switchFromActiveProvider =
1505+
followsCurrent &&
1506+
activeProviderThread !== undefined &&
1507+
activeProviderThread.providerInstanceId !== queuedRun.providerInstanceId;
1508+
const switchPlan =
1509+
selectionChanged || switchFromActiveProvider
1510+
? yield* providerSwitchService
1511+
.plan({
1512+
projection: switchFromActiveProvider
1513+
? {
1514+
...projection,
1515+
thread: {
1516+
...projection.thread,
1517+
modelSelection: {
1518+
...projection.thread.modelSelection,
1519+
instanceId: activeProviderThread.providerInstanceId,
1520+
},
1521+
},
1522+
}
1523+
: projection,
1524+
targetModelSelection: queuedRun.modelSelection,
1525+
})
1526+
.pipe(
1527+
Effect.mapError(
1528+
(cause) =>
1529+
new OrchestratorDispatchError({
1530+
commandId,
1531+
commandType: "message.dispatch",
1532+
cause,
1533+
}),
1534+
),
1535+
)
1536+
: null;
15081537
const canResumeAcrossInstances =
15091538
switchPlan?.instanceChanged === true &&
15101539
switchPlan.transition.type === "restart_and_resume" &&
@@ -1798,7 +1827,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
17981827
summary: activeHandoff.summaryText,
17991828
};
18001829
const executionIdentityEvents: ReadonlyArray<Omit<OrchestrationV2DomainEvent, "id">> =
1801-
storedCheckpointScope === undefined || storedProviderThread.id !== queuedProviderThread.id
1830+
storedCheckpointScope === undefined ||
1831+
storedProviderThread.id !== queuedProviderThread.id ||
1832+
attempt.providerInstanceId !== queuedRun.providerInstanceId
18021833
? [
18031834
{
18041835
type: "checkpoint-scope.created",

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4670,13 +4670,19 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
46704670
}),
46714671
);
46724672

4673-
it.effect(
4674-
"delivers a queued monitor update in the current conversation on the same provider",
4675-
() =>
4673+
it.effect.each([
4674+
["server", modelSelection],
4675+
["provider", modelSelection],
4676+
["provider", { ...modelSelection, instanceId: alternateInstanceId }],
4677+
] as const)(
4678+
"delivers a queued monitor update from %s after conversation selection %j",
4679+
([creationSource, currentSelection]) =>
46764680
Effect.gen(function* () {
46774681
const orchestrator = yield* Orchestrator.OrchestratorV2;
46784682
const eventSink = yield* EventSink.EventSinkV2;
4679-
const threadId = ThreadId.make("runtime-layer-queue-current-conversation");
4683+
const threadId = ThreadId.make(
4684+
`runtime-layer-queue-current-conversation-${creationSource}-${currentSelection.instanceId}`,
4685+
);
46804686
yield* orchestrator.dispatch({
46814687
type: "thread.create",
46824688
commandId: CommandId.make(`${threadId}:create`),
@@ -4700,7 +4706,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
47004706
text: type,
47014707
attachments: [],
47024708
createdBy: "agent",
4703-
creationSource: "server",
4709+
creationSource,
47044710
dispatchMode: { type },
47054711
...(type === "queue_after_active"
47064712
? {
@@ -4723,6 +4729,12 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
47234729
id: ProviderThreadId.make(`${threadId}:current`),
47244730
updatedAt: now,
47254731
};
4732+
yield* orchestrator.dispatch({
4733+
type: "thread.model-selection.set",
4734+
commandId: CommandId.make(`${threadId}:select-current`),
4735+
threadId,
4736+
modelSelection: currentSelection,
4737+
});
47264738
yield* eventSink.write({
47274739
events: [
47284740
{
@@ -4742,23 +4754,32 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
47424754
},
47434755
],
47444756
});
4757+
const expectedThreadId =
4758+
creationSource === "provider" ? queued.providerThreadId : current.id;
47454759
yield* orchestrator.resumeQueuedRuns;
47464760
const after = yield* orchestrator.getThreadProjection(threadId);
47474761
const delivered = after.runs.find((run) => run.id === queued.id)!;
47484762
assert.equal(delivered.status, "starting");
4749-
assert.equal(delivered.providerThreadId, current.id);
4750-
assert.equal(after.thread.activeProviderThreadId, current.id);
4763+
assert.deepEqual(delivered.modelSelection, modelSelection);
4764+
assert.equal(delivered.providerInstanceId, modelSelection.instanceId);
4765+
assert.equal(
4766+
after.attempts.find((attempt) => attempt.id === delivered.activeAttemptId)
4767+
?.providerInstanceId,
4768+
modelSelection.instanceId,
4769+
);
4770+
assert.equal(delivered.providerThreadId, expectedThreadId);
4771+
assert.equal(after.thread.activeProviderThreadId, expectedThreadId);
47514772
assert.equal(
47524773
after.attempts.find((attempt) => attempt.id === delivered.activeAttemptId)
47534774
?.providerThreadId,
4754-
current.id,
4775+
expectedThreadId,
47554776
);
47564777
const root = after.nodes.find((node) => node.id === delivered.rootNodeId)!;
4757-
assert.equal(root.providerThreadId, current.id);
4778+
assert.equal(root.providerThreadId, expectedThreadId);
47584779
assert.equal(
47594780
after.checkpointScopes.find((scope) => scope.id === root.checkpointScopeId)
47604781
?.providerThreadId,
4761-
current.id,
4782+
expectedThreadId,
47624783
);
47634784
}),
47644785
);

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,20 +2234,37 @@ describe("orchestration v2 provider switching", () => {
22342234
),
22352235
);
22362236

2237-
it.live.each(["automatic", "queue.resume"] as const)(
2238-
"delivers a queued PR update on the current provider through %s",
2239-
(delivery) =>
2237+
it.live.each([
2238+
["automatic", "Claude"],
2239+
["queue.resume", "Claude"],
2240+
["automatic", "compatible Codex"],
2241+
["queue.resume", "compatible Codex"],
2242+
] as const)(
2243+
"delivers a queued PR update on the current provider through %s (%s)",
2244+
([delivery, target]) =>
22402245
Effect.scoped(
22412246
Effect.gen(function* () {
22422247
const cwd = yield* checkpointWorkspace(`pr-wake-${delivery}`);
22432248
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
22442249
const started = yield* Deferred.make<void>();
22452250
const release = yield* Deferred.make<void>();
2251+
const compatible = target === "compatible Codex";
22462252
const selection = {
2247-
...CLAUDE_MODEL_SELECTION,
2253+
...(compatible
2254+
? { ...CODEX_MODEL_SELECTION, instanceId: ProviderInstanceId.make("codex-alternate") }
2255+
: CLAUDE_MODEL_SELECTION),
22482256
options: [{ id: "reasoningEffort", value: "low" }],
22492257
};
2250-
const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
2258+
const targetCapabilities = compatible
2259+
? {
2260+
...CodexProviderCapabilitiesV2,
2261+
context: {
2262+
...CodexProviderCapabilitiesV2.context,
2263+
canConsumeHandoffSummaries: false,
2264+
},
2265+
}
2266+
: ClaudeProviderCapabilitiesV2;
2267+
const adapters = [
22512268
makeTestAdapter({
22522269
instanceId: CODEX_MODEL_SELECTION.instanceId,
22532270
driver: CODEX_DRIVER,
@@ -2266,13 +2283,43 @@ describe("orchestration v2 provider switching", () => {
22662283
}),
22672284
makeTestAdapter({
22682285
instanceId: selection.instanceId,
2269-
driver: CLAUDE_DRIVER,
2270-
capabilities: ClaudeProviderCapabilitiesV2,
2286+
driver: compatible ? CODEX_DRIVER : CLAUDE_DRIVER,
2287+
capabilities: targetCapabilities,
22712288
modelSelection: selection,
22722289
responseByRunOrdinal: {},
22732290
capturedTurns,
22742291
}),
2275-
]);
2292+
];
2293+
const layerRegistry = compatible
2294+
? Layer.succeed(
2295+
ProviderAdapterRegistry.ProviderAdapterRegistryV2,
2296+
ProviderAdapterRegistry.ProviderAdapterRegistryV2.of({
2297+
get: (instanceId) => {
2298+
const adapter = adapters.find(
2299+
(candidate) => candidate.instanceId === instanceId,
2300+
);
2301+
return adapter === undefined
2302+
? Effect.fail(
2303+
new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({
2304+
instanceId,
2305+
}),
2306+
)
2307+
: Effect.succeed(adapter);
2308+
},
2309+
list: () => Effect.succeed(adapters.map((adapter) => adapter.instanceId)),
2310+
getMetadata: (instanceId) =>
2311+
Effect.succeed({
2312+
driver: CODEX_DRIVER,
2313+
continuationKey: "codex:shared-native-account-history",
2314+
enabled: true,
2315+
capabilities:
2316+
instanceId === selection.instanceId
2317+
? targetCapabilities
2318+
: CodexProviderCapabilitiesV2,
2319+
}),
2320+
}),
2321+
)
2322+
: ProviderAdapterRegistry.layerFromAdapters(adapters);
22762323
yield* Effect.gen(function* () {
22772324
const orchestrator = yield* Orchestrator.OrchestratorV2;
22782325
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
@@ -2357,7 +2404,7 @@ describe("orchestration v2 provider switching", () => {
23572404
({ event }) =>
23582405
event.type === "run.updated" &&
23592406
event.runId === wake.id &&
2360-
event.payload.status === "completed",
2407+
(event.payload.status === "completed" || event.payload.status === "failed"),
23612408
),
23622409
Stream.runHead,
23632410
);
@@ -2366,11 +2413,12 @@ describe("orchestration v2 provider switching", () => {
23662413
const turns = yield* Ref.get(capturedTurns);
23672414
assert.lengthOf(turns, delivery === "automatic" ? 3 : 2);
23682415
const executed = turns.at(-1)!;
2369-
assert.equal(executed.driver, CLAUDE_DRIVER);
2416+
assert.equal(executed.driver, compatible ? CODEX_DRIVER : CLAUDE_DRIVER);
23702417
assert.include(executed.text, "Review the new PR comments");
23712418
assert.deepEqual(executed.modelSelection, selection);
23722419
assert.deepEqual(after.thread.modelSelection, selection);
23732420
const delivered = after.runs.find((run) => run.id === wake.id)!;
2421+
assert.equal(delivered.status, "completed");
23742422
assert.deepEqual(delivered.modelSelection, selection);
23752423
assert.equal(delivered.providerInstanceId, selection.instanceId);
23762424
assert.equal(delivered.providerThreadId, executed.providerThreadId);
@@ -2379,6 +2427,24 @@ describe("orchestration v2 provider switching", () => {
23792427
assert.equal(executed.providerThreadId, turns[1]!.providerThreadId);
23802428
assert.equal(executed.nativeThreadId, turns[1]!.nativeThreadId);
23812429
}
2430+
if (compatible) {
2431+
assert.equal(executed.nativeThreadId, turns[0]!.nativeThreadId);
2432+
assert.isEmpty(after.contextHandoffs);
2433+
if (delivery === "automatic") {
2434+
assert.equal(delivered.providerThreadId, wake.providerThreadId);
2435+
}
2436+
}
2437+
assert.equal(
2438+
after.providerThreads.find((entry) => entry.id === delivered.providerThreadId)
2439+
?.providerInstanceId,
2440+
selection.instanceId,
2441+
);
2442+
assert.equal(
2443+
after.turnItems.find(
2444+
(entry) => entry.type === "notification" && entry.runId === wake.id,
2445+
)?.providerThreadId,
2446+
delivered.providerThreadId,
2447+
);
23822448
const attempt = after.attempts.find((entry) => entry.id === delivered.activeAttemptId);
23832449
const root = after.nodes.find((entry) => entry.id === delivered.rootNodeId);
23842450
assert.equal(attempt?.providerInstanceId, selection.instanceId);

0 commit comments

Comments
 (0)