Skip to content
TypeScript

fix(queued-message): avoid duplicate queued message entries (#26355)

Yash Singh10e709c33786
diff --git a/apps/mobile/src/lib/threadActivity.ts b/apps/mobile/src/lib/threadActivity.ts
index 03ea87da796..9cdb3b6f8d8 100644
--- a/apps/mobile/src/lib/threadActivity.ts
+++ b/apps/mobile/src/lib/threadActivity.ts
@@ -2191,6 +2191,12 @@ export function buildThreadFeed(
const item = row.item;
if (turnItemIsWorkspacePreparation(item)) continue;
if (item.type === "todo_list" || item.type === "checkpoint") continue;
+ if (
+ item.type === "user_message" &&
+ item.inputIntent === "queued_turn" &&
+ item.status === "pending"
+ )
+ continue;
if (item.type === "user_message" && foldedAnswerMessageIds.has(item.messageId)) continue;
if (
item.type === "run_interrupt_request" ||
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index fa447ef1655..3f76bd6185e 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -1920,6 +1920,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}),
providerThreadId: queuedProviderThread.id,
inputIntent: "queued_turn",
+ status: "completed",
startedAt: now,
completedAt: now,
updatedAt: now,
@@ -5749,6 +5750,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
providerThreadId: queuedProviderThread.id,
providerTurnId: null,
inputIntent: "queued_turn",
+ status: "pending",
+ startedAt: null,
+ completedAt: null,
updatedAt: now,
},
});
diff --git a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
index 7481dcaa41b..d8ac4769e14 100644
--- a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
+++ b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
@@ -339,6 +339,13 @@ it.effect.each(
if (delivery !== null) {
yield* Deferred.succeed(rejectSteer, undefined);
yield* Fiber.join(delivery);
+ const requeued = yield* orchestrator.getThreadProjection(threadId);
+ const item = requeued.turnItems.find(
+ (item) => item.type === "user_message" && item.messageId === messageId,
+ );
+ assert.equal(item?.status, "pending");
+ assert.isNull(item?.startedAt);
+ assert.isNull(item?.completedAt);
}
if (timing === "before dispatch") yield* dispatchSteer;
yield* worker.drain();
@@ -391,6 +398,14 @@ it.effect.each(
]);
assert.equal(steerCalls, timing === "during delivery" ? 1 : 0);
const final = yield* orchestrator.getThreadProjection(threadId);
+ if (!mailbox) {
+ assert.equal(
+ final.turnItems.find(
+ (item) => item.type === "user_message" && item.messageId === messageId,
+ )?.status,
+ "completed",
+ );
+ }
assert.equal(final.messages.filter((message) => message.id === messageId).length, 1);
assert.equal(
final.messages.find((message) => message.id === messageId)?.runId,
@@ -669,6 +684,78 @@ it.effect("sends a selected held message once and preserves the remaining queue
),
);
+it.effect("force sends multiple queued messages and can force send a requeued message again", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
+ "multiple-promoted-messages",
+ );
+ yield* Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
+ const threadId = yield* startFirstTurn;
+ const queue = (text: string, commandId: string) =>
+ orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(commandId),
+ threadId,
+ messageId: MessageId.make(`message:${text}`),
+ text,
+ attachments: [],
+ dispatchMode: { type: "queue_after_active" },
+ createdBy: "user",
+ creationSource: "web",
+ });
+ for (const text of ["one", "two", "remaining"]) yield* queue(text, `queue-${text}`);
+ const promote = Effect.fn("test.promote")(function* (text: string, commandId: string) {
+ const projection = yield* orchestrator.getThreadProjection(threadId);
+ const queued = projection.runs.find(
+ (run) =>
+ run.status === "queued" && run.userMessageId === MessageId.make(`message:${text}`),
+ )!;
+ yield* orchestrator.dispatch({
+ type: "queued-message.promote-to-steer",
+ commandId: CommandId.make(commandId),
+ threadId,
+ queuedRunId: queued.id,
+ targetRunId: started[0]!.runId,
+ });
+ });
+ yield* promote("one", "promote-one");
+ yield* promote("two", "promote-two");
+ yield* worker.drain();
+ assert.deepEqual(steered, ["one", "two"]);
+
+ // The completion-race fallback redispatches the same message into the queue.
+ yield* queue("one", "requeue-one");
+ const requeued = yield* orchestrator.getThreadProjection(threadId);
+ assert.equal(
+ requeued.turnItems.find(
+ (item) => item.type === "user_message" && item.messageId === "message:one",
+ )?.status,
+ "pending",
+ );
+ yield* promote("one", "promote-one-again");
+ yield* worker.drain();
+ assert.deepEqual(steered, ["one", "two", "one"]);
+ assert.lengthOf(started, 1);
+ const final = yield* orchestrator.getThreadProjection(threadId);
+ assert.deepEqual(
+ final.runs.filter((run) => run.status === "queued").map((run) => run.userMessageId),
+ [MessageId.make("message:remaining")],
+ );
+ for (const text of ["one", "two"]) {
+ const items = final.turnItems.filter(
+ (item) => item.type === "user_message" && item.messageId === `message:${text}`,
+ );
+ assert.lengthOf(items, 1);
+ assert.equal(items[0]?.status, "completed");
+ }
+ }).pipe(Effect.provide(layer));
+ }),
+ ),
+);
+
it.effect("keeps a message queued when send now races with a running turn", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/web/src/session-logic.test.ts b/apps/web/src/session-logic.test.ts
index fd10c96aa1b..de213de2f3c 100644
--- a/apps/web/src/session-logic.test.ts
+++ b/apps/web/src/session-logic.test.ts
@@ -672,6 +672,27 @@ describe("V2 session presentation", () => {
scheduledTaskId: ScheduledTaskId.make("task-queued"),
senderThreadId: ThreadId.make("thread-agent-sender"),
} satisfies OrchestrationV2TurnItem;
+ for (const inputIntent of [
+ "queued_turn",
+ "turn_start",
+ "steer",
+ "promoted_queued_to_steer",
+ ] as const) {
+ expect(
+ deriveTimelineEntriesFromVisibleTurnItems({
+ visibleTurnItems: [
+ {
+ position: 0,
+ visibility: "local",
+ sourceThreadId: projection.thread.id,
+ sourceItemId: dispatchedItem.id,
+ item: { ...dispatchedItem, inputIntent, status: "pending" },
+ },
+ ],
+ optimisticMessages: [],
+ }),
+ ).toHaveLength(inputIntent === "queued_turn" ? 0 : 1);
+ }
const promotedEntries = deriveTimelineEntriesFromVisibleTurnItems({
visibleTurnItems: [
{
diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts
index eb61b244075..362d9fb9451 100644
--- a/apps/web/src/session-logic.ts
+++ b/apps/web/src/session-logic.ts
@@ -603,6 +603,12 @@ export function deriveTimelineEntriesFromVisibleTurnItems(
const { item } = row;
if (turnItemIsWorkspacePreparation(item)) continue;
if (item.type === "checkpoint") continue;
+ if (
+ item.type === "user_message" &&
+ item.inputIntent === "queued_turn" &&
+ item.status === "pending"
+ )
+ continue;
if (item.type === "user_message" && foldedAnswerMessageIds.has(item.messageId)) continue;
const createdAt = projectedItemCreatedAt(row);
const attempt = resolveAttempt(item);