Skip to content

Commit 0cb021b

Browse files
committed
fix(composer): queue follow-ups after limits, compaction, and background waits
Queue messages sent while a usage limit blocks the thread, while the provider is compacting, or while the thread is waiting on an awaited command or subagent, instead of starting immediately or leaving the send button stuck.
1 parent 6577c6b commit 0cb021b

11 files changed

Lines changed: 382 additions & 44 deletions

File tree

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
import { resolveThreadWorkingStartedAt } from "@t3tools/client-runtime/state/models";
4141
import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLegacy";
4242
import { restoreAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
43+
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
4344
import { composerContextSendBlockReason, reidentifyComposerContext } from "../lib/composerContext";
4445
import { uuidv4 } from "../lib/uuid";
4546

@@ -379,8 +380,14 @@ export function useThreadComposerState(input: {
379380
item.status === "cancelled"),
380381
);
381382
})();
382-
// A compacting turn cannot take steering input, so follow-ups queue meanwhile.
383-
const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true && !isCompacting;
383+
// Awaited commands/subagents park runtime at idle while the banner still says
384+
// Waiting; treat that like an active turn so follow-ups queue instead of starting.
385+
const isWaitingOnBackgroundWork = backgroundWorkHoldsCompletion(
386+
selectedThreadShell?.pendingBackgroundTasks ?? [],
387+
);
388+
// Compaction and awaited background work cannot take steering input, so follow-ups queue.
389+
const canSteerActiveTurn =
390+
queueWorkflow?.canPromoteToSteer === true && !isCompacting && !isWaitingOnBackgroundWork;
384391
// A stop or a restart holds the queue, completion wakes included, until it
385392
// is resumed; the queue sheet lists only the user's own messages.
386393
const queueHeld = queueWorkflow?.isHeld === true;
@@ -408,7 +415,8 @@ export function useThreadComposerState(input: {
408415
? deriveProviderSubagentStatus(selectedThreadProjection.projection)
409416
: null;
410417

411-
const activeThreadBusy = threadRuntimeIsActive(selectedThreadRuntime);
418+
const activeThreadBusy =
419+
threadRuntimeIsActive(selectedThreadRuntime) || isCompacting || isWaitingOnBackgroundWork;
412420
const interruptibleRunId = threadRuntimeHasInterruptibleRun(selectedThreadRuntime)
413421
? (selectedThreadRuntime?.activeRunId ?? null)
414422
: null;
@@ -715,9 +723,10 @@ export function useThreadComposerState(input: {
715723
// An approval redirect must steer the waiting turn instead of queueing behind it.
716724
const activeTurnDefault = beforeDispatch !== undefined ? "steer" : followUpBehavior;
717725
const followUpAction = resolveComposerDispatchMode({
718-
running: activeThreadBusy && canSteerActiveTurn,
726+
running: activeThreadBusy,
719727
alternateModifier: followUpOverride !== undefined && followUpOverride !== activeTurnDefault,
720728
activeTurnDefault,
729+
canSteer: canSteerActiveTurn,
721730
});
722731
const setupRunning = [selectedThreadActivityRun?.status, selectedThreadRuntime?.status].some(
723732
(status) => status === "preparing" || status === "starting",
@@ -748,10 +757,7 @@ export function useThreadComposerState(input: {
748757
settings: serverConfig.settings,
749758
text,
750759
threadIdle:
751-
!activeThreadBusy &&
752-
!runlessWorkActive &&
753-
!isCompacting &&
754-
selectedThreadQueuedMessages.length === 0,
760+
!activeThreadBusy && !runlessWorkActive && selectedThreadQueuedMessages.length === 0,
755761
now: metadata.createdAt,
756762
});
757763
const messageId = MessageId.make(metadata.messageId);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6104,7 +6104,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
61046104
projection.thread.worktreePath === null
61056105
? latestRun
61066106
: undefined;
6107-
const queueBehindRun = activeRun ?? worktreeMissingRun;
6107+
// A message queued while a usage limit blocks its provider waits for the
6108+
// continuation, like messages queued before the limit, instead of failing on it.
6109+
const usageLimitedRun =
6110+
activeRun === undefined &&
6111+
dispatchMode.type === "queue_after_active" &&
6112+
latestRun !== null &&
6113+
usageLimitHoldsQueue(projection, modelSelection.instanceId)
6114+
? latestRun
6115+
: undefined;
6116+
const queueBehindRun = activeRun ?? worktreeMissingRun ?? usageLimitedRun;
61086117
const pendingMergeBackTransfers = pendingMergeBackTransfersForThread(projection);
61096118
const startsOrQueuesRun =
61106119
dispatchMode.type === "defer_start" ||

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5910,6 +5910,131 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
59105910
}),
59115911
);
59125912

5913+
it.effect("queues a message sent with Queue while a usage limit blocks the thread", () =>
5914+
Effect.gen(function* () {
5915+
const orchestrator = yield* Orchestrator.OrchestratorV2;
5916+
const eventSink = yield* EventSink.EventSinkV2;
5917+
const threadId = ThreadId.make("runtime-layer-queue-after-limit");
5918+
yield* orchestrator.dispatch({
5919+
type: "thread.create",
5920+
commandId: CommandId.make(`${threadId}:create`),
5921+
threadId,
5922+
createdBy: "user",
5923+
creationSource: "web",
5924+
projectId: ProjectId.make(`${threadId}:project`),
5925+
title: "Queue after limit",
5926+
modelSelection,
5927+
runtimeMode: "full-access",
5928+
interactionMode: "default",
5929+
branch: null,
5930+
worktreePath: process.cwd(),
5931+
});
5932+
const send = (index: number, type: "start_immediately" | "queue_after_active") =>
5933+
orchestrator.dispatch({
5934+
type: "message.dispatch",
5935+
commandId: CommandId.make(`${threadId}:${index}`),
5936+
threadId,
5937+
messageId: MessageId.make(`${threadId}:${index}`),
5938+
text: `Prompt ${index}`,
5939+
attachments: [],
5940+
createdBy: "user",
5941+
creationSource: "web",
5942+
dispatchMode: { type },
5943+
});
5944+
yield* send(0, "start_immediately");
5945+
const [limited] = (yield* orchestrator.getThreadProjection(threadId)).runs;
5946+
assert.isDefined(limited);
5947+
const now = yield* DateTime.now;
5948+
yield* eventSink.write({
5949+
events: [
5950+
{
5951+
id: EventId.make(`${threadId}:error`),
5952+
type: "turn-item.updated",
5953+
threadId,
5954+
runId: limited.id,
5955+
nodeId: limited.rootNodeId!,
5956+
providerInstanceId: limited.providerInstanceId,
5957+
occurredAt: now,
5958+
payload: {
5959+
id: TurnItemId.make(`${threadId}:error`),
5960+
type: "error",
5961+
threadId,
5962+
runId: limited.id,
5963+
nodeId: limited.rootNodeId!,
5964+
providerThreadId: limited.providerThreadId,
5965+
providerTurnId: null,
5966+
nativeItemRef: null,
5967+
parentItemId: null,
5968+
ordinal: 2,
5969+
status: "failed",
5970+
title: "Usage limit",
5971+
startedAt: now,
5972+
completedAt: now,
5973+
updatedAt: now,
5974+
failure: {
5975+
class: "usage_limit",
5976+
message: "Usage limit reached",
5977+
code: "usage_limit",
5978+
retryable: null,
5979+
},
5980+
},
5981+
},
5982+
{
5983+
id: EventId.make(`${threadId}:failed`),
5984+
type: "run.updated",
5985+
threadId,
5986+
runId: limited.id,
5987+
providerInstanceId: limited.providerInstanceId,
5988+
occurredAt: now,
5989+
payload: { ...limited, status: "failed", completedAt: now },
5990+
},
5991+
],
5992+
});
5993+
5994+
yield* send(1, "queue_after_active");
5995+
yield* orchestrator.resumeQueuedRuns;
5996+
const runFor = (index: number) =>
5997+
Effect.map(orchestrator.getThreadProjection(threadId), ({ runs }) =>
5998+
runs.find((run) => run.userMessageId === MessageId.make(`${threadId}:${index}`)),
5999+
);
6000+
const queued = yield* runFor(1);
6001+
assert.equal(queued?.status, "queued");
6002+
assert.notEqual(queued?.queueHeld, true);
6003+
6004+
yield* orchestrator.dispatch({
6005+
type: "message.dispatch",
6006+
commandId: CommandId.make(`${threadId}:2`),
6007+
threadId,
6008+
messageId: MessageId.make(`${threadId}:2`),
6009+
manualContinuationOfRunId: limited.id,
6010+
text: "Continue where you left off.",
6011+
attachments: [],
6012+
createdBy: "user",
6013+
creationSource: "web",
6014+
dispatchMode: { type: "start_immediately" },
6015+
});
6016+
const continuation = yield* runFor(2);
6017+
assert.equal(continuation?.status, "starting");
6018+
assert.equal((yield* runFor(1))?.status, "queued");
6019+
const completedAt = yield* DateTime.now;
6020+
yield* eventSink.write({
6021+
events: [
6022+
{
6023+
id: EventId.make(`${threadId}:continuation:completed`),
6024+
type: "run.updated",
6025+
threadId,
6026+
runId: continuation!.id,
6027+
providerInstanceId: continuation!.providerInstanceId,
6028+
occurredAt: completedAt,
6029+
payload: { ...continuation!, status: "completed", startedAt: completedAt, completedAt },
6030+
},
6031+
],
6032+
});
6033+
yield* orchestrator.resumeQueuedRuns;
6034+
assert.equal((yield* runFor(1))?.status, "starting");
6035+
}),
6036+
);
6037+
59136038
it.effect.each([
59146039
["server", modelSelection],
59156040
["provider", modelSelection],

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ import {
4646
resetHeldThreadTimeline,
4747
resolveThreadSwitchTimeline,
4848
timelineHasEphemeralPreviewUrls,
49+
composerSendControlBusy,
4950
createLocalDispatchSnapshot,
5051
deriveCommittedServerUserMessageIds,
5152
deriveComposerSendState,
5253
deriveLockedProvider,
5354
hasServerAcknowledgedLocalDispatch,
55+
sendHoldsLocalDispatch,
5456
waitForRevertedMessage,
5557
} from "./ChatView.logic";
5658

@@ -1141,6 +1143,101 @@ describe("planComposerTurn", () => {
11411143
shouldQueueBehindActiveRun: true,
11421144
});
11431145
});
1146+
1147+
it("queues ordinary follow-ups while compacting, including before the turn reads as running", () => {
1148+
expect(
1149+
planComposerTurn({
1150+
...base,
1151+
isServerThread: true,
1152+
activeMessageCount: 3,
1153+
phase: "connecting",
1154+
dispatchMode: "steer",
1155+
isCompacting: true,
1156+
}),
1157+
).toMatchObject({
1158+
turnDispatchMode: "queue",
1159+
shouldQueueBehindActiveRun: true,
1160+
});
1161+
expect(
1162+
planComposerTurn({
1163+
...base,
1164+
messageText: "/compact",
1165+
isServerThread: true,
1166+
activeMessageCount: 3,
1167+
phase: "connecting",
1168+
isCompacting: true,
1169+
}),
1170+
).toMatchObject({
1171+
turnDispatchMode: "auto",
1172+
shouldQueueBehindActiveRun: false,
1173+
});
1174+
});
1175+
1176+
it("queues follow-ups while waiting on awaited commands or subagents, even when phase is ready", () => {
1177+
expect(
1178+
planComposerTurn({
1179+
...base,
1180+
isServerThread: true,
1181+
activeMessageCount: 3,
1182+
phase: "ready",
1183+
dispatchMode: "steer",
1184+
isWaitingOnBackgroundWork: true,
1185+
}),
1186+
).toMatchObject({
1187+
turnDispatchMode: "queue",
1188+
shouldQueueBehindActiveRun: true,
1189+
});
1190+
});
1191+
});
1192+
1193+
describe("sendHoldsLocalDispatch", () => {
1194+
it("releases the composer for sends that only join the queue", () => {
1195+
expect(
1196+
sendHoldsLocalDispatch({ sendQueuesBehindSetup: false, shouldQueueBehindActiveRun: false }),
1197+
).toBe(true);
1198+
expect(
1199+
sendHoldsLocalDispatch({ sendQueuesBehindSetup: true, shouldQueueBehindActiveRun: false }),
1200+
).toBe(false);
1201+
expect(
1202+
sendHoldsLocalDispatch({ sendQueuesBehindSetup: false, shouldQueueBehindActiveRun: true }),
1203+
).toBe(false);
1204+
});
1205+
});
1206+
1207+
describe("composerSendControlBusy", () => {
1208+
const idle = {
1209+
isSendBusy: false,
1210+
sendQueuesBehindSetup: false,
1211+
isCompacting: false,
1212+
setupQueueSendActive: false,
1213+
queuedRunEditSaving: false,
1214+
isResuming: false,
1215+
};
1216+
1217+
it("spins while a local dispatch is in flight, except during compaction, background wait, or setup queueing", () => {
1218+
expect(composerSendControlBusy({ ...idle, isSendBusy: true })).toBe(true);
1219+
expect(
1220+
composerSendControlBusy({ ...idle, isSendBusy: true, sendQueuesBehindSetup: true }),
1221+
).toBe(false);
1222+
expect(composerSendControlBusy({ ...idle, isSendBusy: true, isCompacting: true })).toBe(false);
1223+
expect(
1224+
composerSendControlBusy({ ...idle, isSendBusy: true, isWaitingOnBackgroundWork: true }),
1225+
).toBe(false);
1226+
});
1227+
1228+
it("still spins for an in-flight queue RPC, queued edit save, or resume", () => {
1229+
expect(composerSendControlBusy({ ...idle, setupQueueSendActive: true })).toBe(true);
1230+
expect(composerSendControlBusy({ ...idle, queuedRunEditSaving: true })).toBe(true);
1231+
expect(composerSendControlBusy({ ...idle, isResuming: true })).toBe(true);
1232+
expect(
1233+
composerSendControlBusy({
1234+
...idle,
1235+
isSendBusy: true,
1236+
isCompacting: true,
1237+
setupQueueSendActive: true,
1238+
}),
1239+
).toBe(true);
1240+
});
11441241
});
11451242

11461243
describe("restorePlanFollowUpComposer", () => {

0 commit comments

Comments
 (0)