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
3 changes: 1 addition & 2 deletions apps/server/src/orchestration-v2/EffectWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ export const layerExecutor: Layer.Layer<
threadId: effect.threadId,
messageId: message.id,
...(error.steerDeclined ? { steerFollowUpOfRunId: run.id } : {}),
...(error.steerDeclined &&
effect.request.steerFollowUpAfterRunOrdinal !== undefined
...(effect.request.steerFollowUpAfterRunOrdinal !== undefined
? {
steerFollowUpAfterRunOrdinal:
effect.request.steerFollowUpAfterRunOrdinal,
Expand Down
11 changes: 6 additions & 5 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8073,10 +8073,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
command.creationSource === "provider" &&
!(yield* stopReachedRun(command, command.threadId, activeRun.id));
let steerFollowUpQueuePosition: number | undefined;
if (
command.steerFollowUpOfRunId !== undefined &&
command.steerFollowUpAfterRunOrdinal !== undefined
) {
if (command.steerFollowUpAfterRunOrdinal !== undefined) {
const afterRunOrdinal = command.steerFollowUpAfterRunOrdinal;
const steerOrdinal =
projection.turnItems.find(
Expand Down Expand Up @@ -8237,7 +8234,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
runId: queued.id,
providerInstanceId: queued.providerInstanceId,
occurredAt: now,
payload: { ...queued, queuePosition: (queued.queuePosition ?? queued.ordinal) + 1 },
payload: {
...queued,
...(heldRunsToRelease.includes(queued) ? { queueHeld: false } : {}),
queuePosition: (queued.queuePosition ?? queued.ordinal) + 1,
},
});
}
}
Expand Down
181 changes: 108 additions & 73 deletions apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,81 +1363,116 @@ it.effect("restarts the turn for an image when native steering takes text only",
}),
);

it.effect("starts a steer that missed the turn on the saved next-turn selection", () =>
Effect.gen(function* () {
const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
"steering-selection-follow-up",
);
yield* Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
const threadId = yield* startFirstTurn;
const first = started[0]!;
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make("steer"),
threadId,
messageId: MessageId.make("message:steer"),
text: "late steer",
attachments: [],
modelSelection: composerSelection,
dispatchMode: { type: "steer_active", targetRunId: first.runId },
createdBy: "user",
creationSource: "web",
});
const settled = yield* orchestrator.streamDomainEvents.pipe(
Stream.filter(
(event) =>
event.type === "run.updated" &&
event.payload.id === first.runId &&
event.payload.status === "waiting",
),
Stream.take(1),
Stream.runDrain,
Effect.forkScoped,
it.effect.each(["complete", "stop-before-end"] as const)(
"starts a steer that missed the turn in its place on the saved next-turn selection: %s",
(timing) =>
Effect.gen(function* () {
const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
`steering-selection-follow-up-${timing}`,
);
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
yield* Queue.offer(events, {
type: "provider_turn.updated",
driver,
providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now },
});
yield* Queue.offer(events, {
type: "turn.terminal",
driver,
providerThreadId: turn.providerThreadId,
providerTurnId: turn.id,
runOrdinal: first.runOrdinal,
status: "completed",
failure: null,
threadDisposition: "reusable",
});
yield* Fiber.join(settled);
yield* worker.drain();
yield* orchestrator.resumeQueuedRuns;
yield* worker.drain();
yield* Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
const threadId = yield* startFirstTurn;
const first = started[0]!;
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make("steer"),
threadId,
messageId: MessageId.make("message:steer"),
text: "late steer",
attachments: [],
modelSelection: composerSelection,
dispatchMode: { type: "steer_active", targetRunId: first.runId },
createdBy: "user",
creationSource: "web",
});
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make("queued-later"),
threadId,
messageId: MessageId.make("message:queued-later"),
text: "queued later",
attachments: [],
dispatchMode: { type: "queue_after_active" },
createdBy: "user",
creationSource: "web",
});
if (timing === "stop-before-end") {
yield* orchestrator.dispatch({
type: "run.interrupt",
commandId: CommandId.make("stop"),
threadId,
runId: first.runId,
holdQueue: true,
});
}
const settled = yield* orchestrator.streamDomainEvents.pipe(
Stream.filter(
(event) =>
event.type === "run.updated" &&
event.payload.id === first.runId &&
event.payload.status === "waiting",
),
Stream.take(1),
Stream.runDrain,
Effect.forkScoped,
);
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
yield* Queue.offer(events, {
type: "provider_turn.updated",
driver,
providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now },
});
yield* Queue.offer(events, {
type: "turn.terminal",
driver,
providerThreadId: turn.providerThreadId,
providerTurnId: turn.id,
runOrdinal: first.runOrdinal,
status: "completed",
failure: null,
threadDisposition: "reusable",
});
yield* Fiber.join(settled);
yield* worker.drain();
if (timing === "stop-before-end") {
const queued = (yield* orchestrator.getThreadProjection(threadId)).runs
.filter((run) => run.status === "queued")
.toSorted((left, right) => (left.queuePosition ?? 0) - (right.queuePosition ?? 0));
assert.deepEqual(
queued.map((run) => [run.userMessageId, run.queueHeld === true]),
[
[MessageId.make("message:steer"), false],
[MessageId.make("message:queued-later"), false],
],
);
return;
}
yield* orchestrator.resumeQueuedRuns;
yield* worker.drain();

const projection = yield* orchestrator.getThreadProjection(threadId);
assert.deepEqual(steered, []);
assert.equal(started.length, 2);
assert.equal(started[1]?.message.messageId, MessageId.make("message:steer"));
assert.deepEqual(started[1]?.modelSelection, composerSelection);
assert.deepEqual(projection.thread.modelSelection, composerSelection);
const message = projection.turnItems.find(
(item) =>
item.type === "user_message" && item.messageId === MessageId.make("message:steer"),
);
assert.equal(message?.runId, started[1]!.runId);
assert.isAbove(
message!.ordinal,
Math.max(
...projection.turnItems
.filter((item) => item.runId === first.runId)
.map((item) => item.ordinal),
),
);
}).pipe(Effect.provide(layer));
}),
const projection = yield* orchestrator.getThreadProjection(threadId);
assert.deepEqual(steered, []);
assert.equal(started.length, 2);
assert.equal(started[1]?.message.messageId, MessageId.make("message:steer"));
assert.deepEqual(started[1]?.modelSelection, composerSelection);
assert.deepEqual(projection.thread.modelSelection, composerSelection);
const message = projection.turnItems.find(
(item) =>
item.type === "user_message" && item.messageId === MessageId.make("message:steer"),
);
assert.equal(message?.runId, started[1]!.runId);
assert.isAbove(
message!.ordinal,
Math.max(
...projection.turnItems
.filter((item) => item.runId === first.runId)
.map((item) => item.ordinal),
),
);
}).pipe(Effect.provide(layer));
}),
);

const declinedSteerTimings = [
Expand Down
Loading