Skip to content

Commit 30fa121

Browse files
committed
fix(server): steers sent between provider turns queue instead of failing
1 parent 96c7896 commit 30fa121

3 files changed

Lines changed: 27 additions & 55 deletions

File tree

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

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -134,51 +134,33 @@ it.each(["preparing", "starting"] as const)(
134134
);
135135

136136
it.each([
137-
["has not started", [], { type: "queue_after_active" }],
138-
[
139-
"is pending",
140-
[{ runAttemptId: activeAttemptId, status: "pending" }],
141-
{ type: "queue_after_active" },
142-
],
143-
[
144-
"has completed",
145-
[{ runAttemptId: activeAttemptId, status: "completed" }],
146-
{ type: "steer_active", targetRunId: activeRunId },
147-
],
137+
["has not started", []],
138+
["is pending", [{ runAttemptId: activeAttemptId, status: "pending" }]],
139+
["has completed", [{ runAttemptId: activeAttemptId, status: "completed" }]],
148140
[
149141
"belongs to a previous attempt",
150142
[{ runAttemptId: RunAttemptId.make("previous-attempt"), status: "running" }],
151-
{ type: "queue_after_active" },
152143
],
153-
] as const)(
154-
"queues automatic delivery when the active run's provider turn %s",
155-
(_, providerTurns, steer) => {
156-
const projection = {
157-
...dispatchProjection(baseCapabilities),
158-
providerTurns,
159-
} as unknown as OrchestrationV2ThreadProjection;
144+
] as const)("queues follow-ups when the active run's provider turn %s", (_, providerTurns) => {
145+
const projection = {
146+
...dispatchProjection(baseCapabilities),
147+
providerTurns,
148+
} as unknown as OrchestrationV2ThreadProjection;
149+
for (const intent of ["auto", "steer"] as const) {
160150
assert.deepEqual(
161-
CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
151+
CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, intent),
162152
{ type: "queue_after_active" },
163153
);
164-
assert.deepEqual(
165-
CommandPolicy.resolveMessageDispatchIntent(
166-
projection,
167-
{ type: "start_immediately" },
168-
"steer",
169-
),
170-
steer,
171-
);
172-
assert.deepEqual(
173-
CommandPolicy.resolveMessageDispatchIntent(
174-
projection,
175-
{ type: "start_immediately" },
176-
"restart",
177-
),
178-
{ type: "restart_active", targetRunId: activeRunId },
179-
);
180-
},
181-
);
154+
}
155+
assert.deepEqual(
156+
CommandPolicy.resolveMessageDispatchIntent(
157+
projection,
158+
{ type: "start_immediately" },
159+
"restart",
160+
),
161+
{ type: "restart_active", targetRunId: activeRunId },
162+
);
163+
});
182164

183165
it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
184166
const projection = dispatchProjection(baseCapabilities);

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,16 @@ export function resolveMessageDispatchIntent(
170170
: mode;
171171
}
172172

173-
export function runAwaitsProviderTurn(
173+
export function runLacksRunningProviderTurn(
174174
projection: Pick<OrchestrationV2ThreadProjection, "providerTurns">,
175175
run: Pick<OrchestrationV2Run, "status" | "activeAttemptId">,
176176
): boolean {
177177
return (
178178
run.status === "preparing" ||
179179
run.status === "starting" ||
180-
(run.status === "running" &&
181-
!projection.providerTurns.some(
182-
(turn) => turn.runAttemptId === run.activeAttemptId && turn.status !== "pending",
183-
))
180+
!projection.providerTurns.some(
181+
(turn) => turn.runAttemptId === run.activeAttemptId && turn.status === "running",
182+
)
184183
);
185184
}
186185

@@ -202,19 +201,10 @@ function resolveDeliveryIntent(
202201
if (deliveryIntent === "restart") {
203202
return { type: "restart_active", targetRunId: activeRun.id };
204203
}
205-
if (runAwaitsProviderTurn(projection, activeRun)) return { type: "queue_after_active" };
204+
if (runLacksRunningProviderTurn(projection, activeRun)) return { type: "queue_after_active" };
206205
if (deliveryIntent === "steer") {
207206
return { type: "steer_active", targetRunId: activeRun.id };
208207
}
209-
// A run stays active after its provider turn finishes. Queue automatic input
210-
// in that gap so steering cannot reject the message.
211-
if (
212-
!projection.providerTurns.some(
213-
(turn) => turn.runAttemptId === activeRun.activeAttemptId && turn.status === "running",
214-
)
215-
) {
216-
return { type: "queue_after_active" };
217-
}
218208

219209
const providerThread = projection.providerThreads.find(
220210
(candidate) => candidate.id === activeRun.providerThreadId,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ import {
9797
goalCommandStopsActiveRun,
9898
isNativeMaintenanceCommand,
9999
resolveMessageDispatchIntent,
100-
runAwaitsProviderTurn,
100+
runLacksRunningProviderTurn,
101101
} from "./CommandPolicy.ts";
102102
import { CommandReceiptStoreV2 } from "./CommandReceiptStore.ts";
103103
import { ContextHandoffServiceV2 } from "./ContextHandoffService.ts";
@@ -5508,7 +5508,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
55085508
command.notification === undefined &&
55095509
!isNativeMaintenanceCommand(command) &&
55105510
(activeRunMessage === undefined || !isNativeMaintenanceCommand(activeRunMessage)) &&
5511-
runAwaitsProviderTurn(projection, activeRun)
5511+
runLacksRunningProviderTurn(projection, activeRun)
55125512
? command.deliveryIntent
55135513
: undefined;
55145514
if (shouldQueue) {

0 commit comments

Comments
 (0)