TypeScript
fix(server): auto-delivered messages queue between provider turns (#15691)
Guillermo Casanovab6295dd801e7
diff --git a/apps/server/src/orchestration-v2/CommandPolicy.test.ts b/apps/server/src/orchestration-v2/CommandPolicy.test.tsindex d3c9f38a495..824076a93a1 100644--- a/apps/server/src/orchestration-v2/CommandPolicy.test.ts+++ b/apps/server/src/orchestration-v2/CommandPolicy.test.ts@@ -7,6 +7,7 @@ import {ProviderInstanceId,ProviderSessionId,ProviderThreadId,+ RunAttemptId,RunId,ThreadId,} from "@t3tools/contracts";@@ -20,6 +21,7 @@ import * as CommandPolicy from "./CommandPolicy.ts";const commandId = CommandId.make("command-policy-test");const threadId = ThreadId.make("command-policy-thread");const activeRunId = RunId.make("command-policy-active-run");+const activeAttemptId = RunAttemptId.make("command-policy-active-attempt");const userMessageId = MessageId.make("command-policy-user-message");const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;@@ -39,7 +41,15 @@ function dispatchProjection(runs:sessionCapabilities === undefined? []- : [{ id: activeRunId, status: "running", providerThreadId, userMessageId }],+ : [+ {+ id: activeRunId,+ status: "running",+ providerThreadId,+ activeAttemptId,+ userMessageId,+ },+ ],messages: [{ id: userMessageId, text: "first", attachments: [] }],providerThreads:sessionCapabilities === undefined ? [] : [{ id: providerThreadId, providerSessionId }],@@ -47,6 +57,10 @@ function dispatchProjection(sessionCapabilities === undefined? []: [{ id: providerSessionId, capabilities: sessionCapabilities }],+ providerTurns:+ sessionCapabilities === undefined+ ? []+ : [{ runAttemptId: activeAttemptId, status: "running" }],} as unknown as OrchestrationV2ThreadProjection;}@@ -120,6 +134,37 @@ it.each(["preparing", "starting"] as const)(},);+it.each([+ ["has not started", []],+ ["is pending", [{ runAttemptId: activeAttemptId, status: "pending" }]],+ ["has completed", [{ runAttemptId: activeAttemptId, status: "completed" }]],+ [+ "belongs to a previous attempt",+ [{ runAttemptId: RunAttemptId.make("previous-attempt"), status: "running" }],+ ],+])("queues only automatic delivery when the active run's provider turn %s", (_, providerTurns) => {+ const projection = {+ ...dispatchProjection(baseCapabilities),+ providerTurns,+ } as unknown as OrchestrationV2ThreadProjection;+ assert.deepEqual(+ CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),+ { type: "queue_after_active" },+ );+ assert.deepEqual(+ CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),+ { type: "steer_active", targetRunId: activeRunId },+ );+ assert.deepEqual(+ CommandPolicy.resolveMessageDispatchIntent(+ projection,+ { type: "start_immediately" },+ "restart",+ ),+ { type: "restart_active", targetRunId: activeRunId },+ );+});+it("targets the latest active run for explicit steer and restart intent", () => {const projection = dispatchProjection(baseCapabilities);assert.deepEqual(diff --git a/apps/server/src/orchestration-v2/CommandPolicy.ts b/apps/server/src/orchestration-v2/CommandPolicy.tsindex 751ce08f72e..923542d877d 100644--- a/apps/server/src/orchestration-v2/CommandPolicy.ts+++ b/apps/server/src/orchestration-v2/CommandPolicy.ts@@ -165,7 +165,15 @@ function resolveDeliveryIntent(if (deliveryIntent === "restart") {return { type: "restart_active", targetRunId: activeRun.id };}- if (activeRun.status === "preparing" || activeRun.status === "starting") {+ // A run can be active before its provider turn starts or after it finishes.+ // Queue automatic input in those gaps so steering cannot reject the message.+ if (+ activeRun.status === "preparing" ||+ activeRun.status === "starting" ||+ !projection.providerTurns.some(+ (turn) => turn.runAttemptId === activeRun.activeAttemptId && turn.status === "running",+ )+ ) {return { type: "queue_after_active" };}