Skip to content

Commit 7b48ba2

Browse files
committed
fix(queue): send selected held messages when a thread is stopped
1 parent 3e250a8 commit 7b48ba2

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":
@@ -1263,13 +1264,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12631264
const failQueuedRunStart = Effect.fn("Orchestrator.failQueuedRunStart")(function* (
12641265
threadId: ThreadId,
12651266
cause: unknown,
1267+
queuedRunId?: RunId,
12661268
) {
12671269
const projection = yield* projectionStore.getThreadRecords(
12681270
threadId,
12691271
["runs", "nodes", "attempts", "providerThreads", "turnItems", "messages"],
12701272
{ turnItemTypes: [], messageRoles: ["user"] },
12711273
);
1272-
const queuedRun = nextQueuedRun(projection);
1274+
const queuedRun =
1275+
queuedRunId === undefined
1276+
? nextQueuedRun(projection)
1277+
: projection.runs.find((run) => run.id === queuedRunId && run.status === "queued");
12731278
if (queuedRun === undefined) return;
12741279
const now = yield* DateTime.now;
12751280
const rootNode = projection.nodes.find((node) => node.id === queuedRun.rootNodeId);
@@ -1370,7 +1375,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13701375
const startNextQueuedRun = Effect.fn("Orchestrator.startNextQueuedRun")(
13711376
function* (
13721377
threadId: ThreadId,
1373-
options?: { readonly failedRunId?: RunId; readonly endedRunId?: RunId },
1378+
options?: {
1379+
readonly failedRunId?: RunId;
1380+
readonly endedRunId?: RunId;
1381+
readonly queuedRunId?: RunId;
1382+
},
13741383
) {
13751384
if (!(yield* projectionStore.canStartQueuedRun(threadId))) return;
13761385
const projection = yield* readCommandProjection(threadId);
@@ -1383,7 +1392,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13831392
return;
13841393
}
13851394

1386-
const storedQueuedRun = nextQueuedRun(projection);
1395+
const storedQueuedRun =
1396+
options?.queuedRunId === undefined
1397+
? nextQueuedRun(projection)
1398+
: projection.runs.find(
1399+
(run) => run.id === options.queuedRunId && run.status === "queued",
1400+
);
13871401
if (storedQueuedRun === undefined) {
13881402
return;
13891403
}
@@ -2073,8 +2087,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
20732087
],
20742088
);
20752089
},
2076-
(effect, threadId, _options?) =>
2077-
effect.pipe(Effect.catch((cause) => failQueuedRunStart(threadId, cause))),
2090+
(effect, threadId, options?) =>
2091+
effect.pipe(
2092+
Effect.catch((cause) => failQueuedRunStart(threadId, cause, options?.queuedRunId)),
2093+
),
20782094
);
20792095

20802096
const resumeQueuedRuns = Effect.gen(function* () {
@@ -11371,6 +11387,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1137111387
case "queued-message.promote-to-steer":
1137211388
yield* dispatchQueuedMessagePromoteToSteer(command, events, effects);
1137311389
break;
11390+
case "queued-run.start":
1137411391
case "queue.resume": {
1137511392
const projection = yield* loadProjectionForCommand(
1137611393
command,
@@ -11384,7 +11401,30 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1138411401
cause: `Thread ${command.threadId} is not active.`,
1138511402
});
1138611403
}
11387-
const nextRun = nextQueuedRun(projection);
11404+
const selectedRunId = command.type === "queued-run.start" ? command.runId : undefined;
11405+
const selectedRun =
11406+
selectedRunId === undefined
11407+
? undefined
11408+
: projection.runs.find((run) => run.id === selectedRunId && run.status === "queued");
11409+
if (selectedRunId !== undefined) {
11410+
const selectedMessage = projection.messages.find(
11411+
(message) => message.id === selectedRun?.userMessageId,
11412+
);
11413+
if (
11414+
selectedRun === undefined ||
11415+
selectedMessage === undefined ||
11416+
selectedMessage.delegatedCompletion !== undefined ||
11417+
selectedMessage.notification !== undefined ||
11418+
projection.runs.some(isBlockingRun)
11419+
) {
11420+
return yield* new OrchestratorDispatchError({
11421+
commandId: command.commandId,
11422+
commandType: command.type,
11423+
cause: "A queued user message can only be sent now while the thread is stopped.",
11424+
});
11425+
}
11426+
}
11427+
const nextRun = selectedRun ?? nextQueuedRun(projection);
1138811428
if (
1138911429
usageLimitHoldsQueue(
1139011430
projection,
@@ -11527,8 +11567,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1152711567
}),
1152811568
),
1152911569
);
11530-
if (command.type === "queue.resume") {
11531-
yield* mapDispatchError(command)(startNextQueuedRun(command.threadId));
11570+
if (command.type === "queue.resume" || command.type === "queued-run.start") {
11571+
yield* mapDispatchError(command)(
11572+
startNextQueuedRun(
11573+
command.threadId,
11574+
command.type === "queued-run.start" ? { queuedRunId: command.runId } : undefined,
11575+
),
11576+
);
1153211577
}
1153311578
return {
1153411579
sequence: receipt.resultSequence,
@@ -11660,8 +11705,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1166011705
detail: committed.receipt.error ?? "Previously rejected.",
1166111706
});
1166211707
}
11663-
if (command.type === "queue.resume") {
11664-
yield* mapDispatchError(command)(startNextQueuedRun(command.threadId));
11708+
if (command.type === "queue.resume" || command.type === "queued-run.start") {
11709+
yield* mapDispatchError(command)(
11710+
startNextQueuedRun(
11711+
command.threadId,
11712+
command.type === "queued-run.start" ? { queuedRunId: command.runId } : undefined,
11713+
),
11714+
);
1166511715
}
1166611716
if (command.type === "notification.delivery.accept") {
1166711717
yield* mapDispatchError(command)(offerDelegatedCompletionDeliveries(command.threadId));

0 commit comments

Comments
 (0)