Skip to content

Commit 96f09c6

Browse files
authored
fix(queue): send selected held messages when a thread is stopped (#22790)
1 parent 1688f96 commit 96f09c6

14 files changed

Lines changed: 341 additions & 44 deletions

File tree

‎apps/mobile/src/features/threads/ThreadQueueControl.tsx‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
149149
void Haptics.selectionAsync();
150150
if (action === "remove") {
151151
await cancel(buildCancelQueuedRunCommand({ ...target, runId }));
152+
} else if (workflow?.canSendNow) {
153+
await resume({ ...target, input: { threadId: target.threadId, runId } });
152154
} else if (workflow?.activeRun && workflow.canPromoteToSteer) {
153155
await promote({
154156
...target,
@@ -221,6 +223,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
221223
const controls = resolveThreadQueueRowControls({
222224
busy: busyRunId !== null || draggedRunId !== null,
223225
canPromoteToSteer: workflow?.canPromoteToSteer ?? false,
226+
canSendNow: workflow?.canSendNow ?? false,
224227
canReorder: workflow?.canReorder ?? false,
225228
index,
226229
isEditing: editing?.runId === run.id,
@@ -326,12 +329,12 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
326329
accessibilityLabel={`Actions for queued message ${index + 1}`}
327330
shouldOpenOnLongPress
328331
actions={[
329-
...(workflow?.canPromoteToSteer
332+
...(workflow?.canPromoteToSteer || workflow?.canSendNow
330333
? [
331334
{
332335
id: "steer",
333-
title: "Steer now",
334-
attributes: { disabled: !controls.canSteer },
336+
title: workflow?.canSendNow ? "Send now" : "Steer now",
337+
attributes: { disabled: !controls.canSend },
335338
image: Platform.OS === "ios" ? "arrow.turn.left.up" : "arrow_upward",
336339
},
337340
]
@@ -385,16 +388,20 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
385388
Editing
386389
</Text>
387390
) : null}
388-
{workflow?.canPromoteToSteer ? (
391+
{workflow?.canPromoteToSteer || workflow?.canSendNow ? (
389392
<Pressable
390393
accessibilityRole="button"
391-
accessibilityLabel={`Steer with message ${index + 1} now`}
392-
disabled={!controls.canSteer}
394+
accessibilityLabel={
395+
workflow?.canSendNow
396+
? `Send message ${index + 1} now`
397+
: `Steer with message ${index + 1} now`
398+
}
399+
disabled={!controls.canSend}
393400
onPress={() => void act(run.id, "steer")}
394401
className="h-8 shrink-0 justify-center rounded-full bg-primary px-3 active:opacity-70 disabled:opacity-40"
395402
>
396403
<Text className="font-t3-medium text-xs text-primary-foreground">
397-
Steer
404+
{workflow?.canSendNow ? "Send now" : "Steer"}
398405
</Text>
399406
</Pressable>
400407
) : null}

‎apps/mobile/src/features/threads/threadQueueControlPresentation.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ import {
77
} from "./threadQueueControlPresentation";
88

99
describe("threadQueueControlPresentation", () => {
10+
it.each([
11+
{ busy: false, isEditing: false, expected: true },
12+
{ busy: true, isEditing: false, expected: false },
13+
{ busy: false, isEditing: true, expected: false },
14+
])(
15+
"gates sending a stopped thread's queued message with $busy busy and $isEditing editing",
16+
({ busy, isEditing, expected }) => {
17+
const controls = resolveThreadQueueRowControls({
18+
busy,
19+
isEditing,
20+
canPromoteToSteer: false,
21+
canSendNow: true,
22+
canReorder: false,
23+
index: 0,
24+
queuedCount: 1,
25+
text: "Queued message",
26+
});
27+
28+
expect(controls.canSend).toBe(expected);
29+
},
30+
);
31+
1032
it("shows an assistant quote as its quoted text", () => {
1133
const href = formatAssistantCitationHref({
1234
version: 1,
@@ -22,6 +44,7 @@ describe("threadQueueControlPresentation", () => {
2244
const controls = resolveThreadQueueRowControls({
2345
busy: false,
2446
canPromoteToSteer: true,
47+
canSendNow: false,
2548
canReorder: true,
2649
index: 0,
2750
queuedCount: 1,
@@ -37,6 +60,7 @@ describe("threadQueueControlPresentation", () => {
3760
const editing = resolveThreadQueueRowControls({
3861
busy: false,
3962
canPromoteToSteer: true,
63+
canSendNow: false,
4064
canReorder: true,
4165
index: 1,
4266
isEditing: true,
@@ -46,7 +70,7 @@ describe("threadQueueControlPresentation", () => {
4670

4771
expect(editing.isEditing).toBe(true);
4872
expect(editing.canEdit).toBe(false);
49-
expect(editing.canSteer).toBe(false);
73+
expect(editing.canSend).toBe(false);
5074
expect(editing.canMoveUp).toBe(true);
5175
expect(editing.canDismiss).toBe(true);
5276
});

‎apps/mobile/src/features/threads/threadQueueControlPresentation.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ThreadQueueRowControls {
88
readonly canEdit: boolean;
99
readonly canMoveDown: boolean;
1010
readonly canMoveUp: boolean;
11-
readonly canSteer: boolean;
11+
readonly canSend: boolean;
1212
readonly dismissAccessibilityLabel: string;
1313
readonly displayText: string;
1414
readonly isEditing: boolean;
@@ -17,6 +17,7 @@ export interface ThreadQueueRowControls {
1717
export function resolveThreadQueueRowControls(input: {
1818
readonly busy: boolean;
1919
readonly canPromoteToSteer: boolean;
20+
readonly canSendNow: boolean;
2021
readonly canReorder: boolean;
2122
readonly index: number;
2223
readonly isEditing?: boolean;
@@ -33,9 +34,9 @@ export function resolveThreadQueueRowControls(input: {
3334
canEdit: mutationEnabled && !isEditing,
3435
canMoveDown: mutationEnabled && input.canReorder && input.index < input.queuedCount - 1,
3536
canMoveUp: mutationEnabled && input.canReorder && input.index > 0,
36-
canSteer: mutationEnabled && input.canPromoteToSteer && !isEditing,
37+
canSend: mutationEnabled && (input.canPromoteToSteer || input.canSendNow) && !isEditing,
3738
dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
38-
// Edit and Steer keep the original text so quoted citations stay linked.
39+
// Editing and sending keep the original text so quoted citations stay linked.
3940
displayText: assistantCitationsToPlainText(input.text),
4041
isEditing,
4142
};

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

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
477477
case "subagent.stop":
478478
case "run.interrupt":
479479
case "queued-message.promote-to-steer":
480+
case "queued-run.start":
480481
case "queue.resume":
481482
case "queued-run.reorder":
482483
case "queued-run.cancel":
@@ -1277,13 +1278,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12771278
const failQueuedRunStart = Effect.fn("Orchestrator.failQueuedRunStart")(function* (
12781279
threadId: ThreadId,
12791280
cause: unknown,
1281+
queuedRunId?: RunId,
12801282
) {
12811283
const projection = yield* projectionStore.getThreadRecords(
12821284
threadId,
12831285
["runs", "nodes", "attempts", "providerThreads", "turnItems", "messages"],
12841286
{ turnItemTypes: [], messageRoles: ["user"] },
12851287
);
1286-
const queuedRun = nextQueuedRun(projection);
1288+
const queuedRun =
1289+
queuedRunId === undefined
1290+
? nextQueuedRun(projection)
1291+
: projection.runs.find((run) => run.id === queuedRunId && run.status === "queued");
12871292
if (queuedRun === undefined) return;
12881293
const now = yield* DateTime.now;
12891294
const rootNode = projection.nodes.find((node) => node.id === queuedRun.rootNodeId);
@@ -1384,7 +1389,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13841389
const startNextQueuedRun = Effect.fn("Orchestrator.startNextQueuedRun")(
13851390
function* (
13861391
threadId: ThreadId,
1387-
options?: { readonly failedRunId?: RunId; readonly endedRunId?: RunId },
1392+
options?: {
1393+
readonly failedRunId?: RunId;
1394+
readonly endedRunId?: RunId;
1395+
readonly queuedRunId?: RunId;
1396+
},
13881397
) {
13891398
if (!(yield* projectionStore.canStartQueuedRun(threadId))) return;
13901399
const projection = yield* readCommandProjection(threadId);
@@ -1397,7 +1406,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13971406
return;
13981407
}
13991408

1400-
const storedQueuedRun = nextQueuedRun(projection);
1409+
const storedQueuedRun =
1410+
options?.queuedRunId === undefined
1411+
? nextQueuedRun(projection)
1412+
: projection.runs.find(
1413+
(run) => run.id === options.queuedRunId && run.status === "queued",
1414+
);
14011415
if (storedQueuedRun === undefined) {
14021416
return;
14031417
}
@@ -2087,8 +2101,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
20872101
],
20882102
);
20892103
},
2090-
(effect, threadId, _options?) =>
2091-
effect.pipe(Effect.catch((cause) => failQueuedRunStart(threadId, cause))),
2104+
(effect, threadId, options?) =>
2105+
effect.pipe(
2106+
Effect.catch((cause) => failQueuedRunStart(threadId, cause, options?.queuedRunId)),
2107+
),
20922108
);
20932109

20942110
const resumeQueuedRuns = Effect.gen(function* () {
@@ -11423,6 +11439,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1142311439
case "queued-message.promote-to-steer":
1142411440
yield* dispatchQueuedMessagePromoteToSteer(command, events, effects);
1142511441
break;
11442+
case "queued-run.start":
1142611443
case "queue.resume": {
1142711444
const projection = yield* loadProjectionForCommand(
1142811445
command,
@@ -11436,7 +11453,30 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1143611453
cause: `Thread ${command.threadId} is not active.`,
1143711454
});
1143811455
}
11439-
const nextRun = nextQueuedRun(projection);
11456+
const selectedRunId = command.type === "queued-run.start" ? command.runId : undefined;
11457+
const selectedRun =
11458+
selectedRunId === undefined
11459+
? undefined
11460+
: projection.runs.find((run) => run.id === selectedRunId && run.status === "queued");
11461+
if (selectedRunId !== undefined) {
11462+
const selectedMessage = projection.messages.find(
11463+
(message) => message.id === selectedRun?.userMessageId,
11464+
);
11465+
if (
11466+
selectedRun === undefined ||
11467+
selectedMessage === undefined ||
11468+
selectedMessage.delegatedCompletion !== undefined ||
11469+
selectedMessage.notification !== undefined ||
11470+
projection.runs.some(isBlockingRun)
11471+
) {
11472+
return yield* new OrchestratorDispatchError({
11473+
commandId: command.commandId,
11474+
commandType: command.type,
11475+
cause: "A queued user message can only be sent now while the thread is stopped.",
11476+
});
11477+
}
11478+
}
11479+
const nextRun = selectedRun ?? nextQueuedRun(projection);
1144011480
if (
1144111481
usageLimitHoldsQueue(
1144211482
projection,
@@ -11579,8 +11619,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1157911619
}),
1158011620
),
1158111621
);
11582-
if (command.type === "queue.resume") {
11583-
yield* mapDispatchError(command)(startNextQueuedRun(command.threadId));
11622+
if (command.type === "queue.resume" || command.type === "queued-run.start") {
11623+
yield* mapDispatchError(command)(
11624+
startNextQueuedRun(
11625+
command.threadId,
11626+
command.type === "queued-run.start" ? { queuedRunId: command.runId } : undefined,
11627+
),
11628+
);
1158411629
}
1158511630
return {
1158611631
sequence: receipt.resultSequence,
@@ -11712,8 +11757,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1171211757
detail: committed.receipt.error ?? "Previously rejected.",
1171311758
});
1171411759
}
11715-
if (command.type === "queue.resume") {
11716-
yield* mapDispatchError(command)(startNextQueuedRun(command.threadId));
11760+
if (command.type === "queue.resume" || command.type === "queued-run.start") {
11761+
yield* mapDispatchError(command)(
11762+
startNextQueuedRun(
11763+
command.threadId,
11764+
command.type === "queued-run.start" ? { queuedRunId: command.runId } : undefined,
11765+
),
11766+
);
1171711767
}
1171811768
if (command.type === "notification.delivery.accept") {
1171911769
yield* mapDispatchError(command)(offerDelegatedCompletionDeliveries(command.threadId));

0 commit comments

Comments
 (0)