Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ function nextQueuedRun(
return queuedRunsInDeliveryOrder(projection).find((run) => run.queueHeld !== true);
}

// Delegated completions, monitor notifications and unpinned scheduled deliveries never choose a
// Delegated completions, monitor notifications and queued messages that name no model never choose a
// model, so a queued one runs on the thread's current pick rather than the one frozen at queue time.
function followsCurrentConversation(
message:
Expand Down Expand Up @@ -3564,7 +3564,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
messageId: message.id,
steerFollowUpOfRunId: run.id,
steerFollowUpAfterRunOrdinal: afterRunOrdinal,
modelSelection: steer?.modelSelection ?? run.modelSelection,
...(message.followsThreadSelection === true
? {}
: { modelSelection: steer?.modelSelection ?? run.modelSelection }),
text: message.text,
...(message.context ? { context: message.context } : {}),
attachments: message.attachments,
Expand Down Expand Up @@ -7881,6 +7883,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
// restart command that stops the active run goes first, so nothing queued applies before it.
// Buffered provider wakes drain output their own provider already produced.
const incomingMessage = { ...command, delegatedCompletion };
// A message that names no model runs on the thread's pick when it starts, not a copy from now.
const followsThreadSelection =
command.modelSelection === undefined &&
(command.scheduledTaskId !== undefined ||
(shouldQueue &&
!followsCurrentConversation(incomingMessage) &&
!isBufferedProviderNotification(incomingMessage)));
if (
shouldQueue &&
// A steer whose run ended keeps the thread's selection, as it did before it queued.
Expand Down Expand Up @@ -8148,9 +8157,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...(command.senderThreadId === undefined ? {} : { senderThreadId: command.senderThreadId }),
...(command.linkedSender === undefined ? {} : { linkedSender: command.linkedSender }),
...(command.idleCompaction === undefined ? {} : { idleCompaction: command.idleCompaction }),
...(command.scheduledTaskId !== undefined && command.modelSelection === undefined
? { followsThreadSelection: true as const }
: {}),
...(followsThreadSelection ? { followsThreadSelection: true as const } : {}),
id: command.messageId,
threadId: command.threadId,
runId,
Expand Down Expand Up @@ -8573,9 +8580,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...(command.senderThreadId === undefined ? {} : { senderThreadId: command.senderThreadId }),
...(command.linkedSender === undefined ? {} : { linkedSender: command.linkedSender }),
...(command.idleCompaction === undefined ? {} : { idleCompaction: command.idleCompaction }),
...(command.scheduledTaskId !== undefined && command.modelSelection === undefined
? { followsThreadSelection: true as const }
: {}),
...(followsThreadSelection ? { followsThreadSelection: true as const } : {}),
id: command.messageId,
threadId: command.threadId,
runId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,74 @@ it.effect("steers a promoted scheduled delivery with the thread's current select
),
);

it.effect("steers a deferred agent message on a pick made while it waited", () =>
Effect.scoped(
Effect.gen(function* () {
const { started, steered, steeredSelections, layer, startFirstTurn, events } =
yield* nextTurnSelectionHarness("deferred-agent-steer-selection");
yield* Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
const threadId = yield* startFirstTurn;
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
yield* (yield* EventSink.EventSinkV2).write({
events: [
{
id: EventId.make("turn-not-ready"),
type: "provider-turn.updated",
threadId,
occurredAt: yield* DateTime.now,
payload: { ...turn, status: "pending" },
},
],
});
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make("agent-steer"),
threadId,
messageId: MessageId.make("message:agent-steer"),
text: "agent steer",
attachments: [],
dispatchMode: { type: "steer_active", targetRunId: started[0]!.runId },
createdBy: "agent",
creationSource: "mcp",
});
const deferred = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
(run) => run.status === "queued",
)!;
assert.equal(deferred.steerWhenReadyRunId, started[0]!.runId);
yield* orchestrator.dispatch({
type: "thread.model-selection.set",
commandId: CommandId.make("select-composer"),
threadId,
modelSelection: composerSelection,
});
const promoted = yield* orchestrator.streamDomainEvents.pipe(
Stream.filter(
(event) =>
event.type === "run.updated" &&
event.payload.id === deferred.id &&
event.payload.status === "cancelled",
),
Stream.take(1),
Stream.runDrain,
Effect.forkScoped,
);
yield* Queue.offer(events, { type: "provider_turn.updated", driver, providerTurn: turn });
yield* Fiber.join(promoted);
yield* worker.drain();

assert.lengthOf(steered, 1);
assert.deepEqual(steeredSelections, [composerSelection]);
assert.deepEqual(
(yield* orchestrator.getThreadProjection(threadId)).thread.modelSelection,
composerSelection,
);
}).pipe(Effect.provide(layer));
}),
),
);

const claudeSelection = {
instanceId: ProviderInstanceId.make("claudeAgent"),
model: "claude-test",
Expand Down
81 changes: 81 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10051,6 +10051,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);

it.effect(
"starts a queued agent message that names no model on a pick made while it waited",
() =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const eventSink = yield* EventSink.EventSinkV2;
const threadId = ThreadId.make("runtime-layer-queued-agent-message-later-pick");
const pickedSelection = { ...modelSelection, model: "gpt-5.5" };
yield* orchestrator.dispatch({
type: "thread.create",
commandId: CommandId.make(`${threadId}:create`),
threadId,
projectId: ProjectId.make(`${threadId}:project`),
createdBy: "user",
creationSource: "web",
title: "Pick while an agent message waits",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: process.cwd(),
});
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`${threadId}:active`),
threadId,
messageId: MessageId.make(`${threadId}:active`),
text: "active",
attachments: [],
modelSelection,
dispatchMode: { type: "start_immediately" },
createdBy: "user",
creationSource: "web",
});
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`${threadId}:agent`),
threadId,
messageId: MessageId.make(`${threadId}:agent`),
text: "Message from another thread",
attachments: [],
dispatchMode: { type: "queue_after_active" },
createdBy: "agent",
creationSource: "mcp",
});
yield* orchestrator.dispatch({
type: "thread.model-selection.set",
commandId: CommandId.make(`${threadId}:pick`),
threadId,
modelSelection: pickedSelection,
});
const [active, agent] = (yield* orchestrator.getThreadProjection(threadId)).runs;
assert.isDefined(active);
assert.isDefined(agent);
const now = yield* DateTime.now;
const current = yield* orchestrator.getThreadProjection(threadId);
yield* eventSink.write({
events: [
{
id: EventId.make(`${active.id}:completed`),
type: "run.updated",
threadId,
runId: active.id,
occurredAt: now,
payload: {
...current.runs.find((run) => run.id === active.id)!,
status: "completed",
completedAt: now,
},
},
],
});
yield* orchestrator.resumeQueuedRuns;
const after = yield* orchestrator.getThreadProjection(threadId);
const delivered = after.runs.find((run) => run.id === agent.id)!;
assert.equal(delivered.status, "starting");
assert.deepEqual(after.thread.modelSelection, pickedSelection);
assert.deepEqual(delivered.modelSelection, pickedSelection);
}),
);

it.effect("keeps a buffered provider wake on its own provider behind a queued pick", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ export const OrchestrationV2ConversationMessage = Schema.Struct({
notification: Schema.optional(OrchestrationV2Notification),
...OrchestrationV2CreationFields,
scheduledTaskId: Schema.optional(ScheduledTaskId),
/** A scheduled delivery that named no model; it runs on the thread's selection at start. */
/** A message that named no model; it runs on the thread's selection at start. */
followsThreadSelection: Schema.optional(Schema.Literal(true)),
senderThreadId: Schema.optional(ThreadId),
linkedSender: Schema.optional(OrchestrationV2LinkedSender),
Expand Down
Loading