Skip to content

Commit 0811501

Browse files
fix(v2): queue messages sent while context compaction runs
1 parent a7d42df commit 0811501

8 files changed

Lines changed: 171 additions & 21 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CommandId,
44
type OrchestrationV2ProviderCapabilities,
55
type OrchestrationV2ThreadProjection,
6+
MessageId,
67
ProviderInstanceId,
78
ProviderSessionId,
89
ProviderThreadId,
@@ -136,6 +137,58 @@ it("targets the latest active run for explicit steer and restart intent", () =>
136137
);
137138
});
138139

140+
it("queues a message sent while a compaction run is active", () => {
141+
const messageId = MessageId.make("command-policy-compaction-message");
142+
const base = dispatchProjection(baseCapabilities);
143+
const projection = {
144+
...base,
145+
runs: base.runs.map((run) => ({ ...run, userMessageId: messageId })),
146+
messages: [{ id: messageId, role: "user", text: "/compact", attachments: [] }],
147+
} as unknown as OrchestrationV2ThreadProjection;
148+
const queueAfterActive = { type: "queue_after_active" };
149+
assert.deepEqual(
150+
resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
151+
queueAfterActive,
152+
);
153+
assert.deepEqual(
154+
resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
155+
queueAfterActive,
156+
);
157+
assert.deepEqual(
158+
resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "restart"),
159+
queueAfterActive,
160+
);
161+
assert.deepEqual(
162+
resolveMessageDispatchIntent(projection, {
163+
type: "steer_active",
164+
targetRunId: activeRunId,
165+
}),
166+
queueAfterActive,
167+
);
168+
});
169+
170+
it("keeps steering an ordinary run alongside a compacted message in history", () => {
171+
const messageId = MessageId.make("command-policy-ordinary-message");
172+
const base = dispatchProjection(baseCapabilities);
173+
const projection = {
174+
...base,
175+
runs: base.runs.map((run) => ({ ...run, userMessageId: messageId })),
176+
messages: [
177+
{ id: messageId, role: "user", text: "ship it", attachments: [] },
178+
{
179+
id: MessageId.make("command-policy-earlier-compaction"),
180+
role: "user",
181+
text: "/compact",
182+
attachments: [],
183+
},
184+
],
185+
} as unknown as OrchestrationV2ThreadProjection;
186+
assert.deepEqual(
187+
resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
188+
{ type: "steer_active", targetRunId: activeRunId },
189+
);
190+
});
191+
139192
const layer = it.layer(commandPolicyLayer);
140193

141194
layer("CommandPolicyV2", (it) => {

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ChatAttachment,
23
CommandId,
34
ModelSelection,
45
type OrchestrationV2Command,
@@ -115,13 +116,44 @@ type MessageDispatchMode = Extract<
115116
{ readonly type: "message.dispatch" }
116117
>["dispatchMode"];
117118

119+
/** Native commands the provider executes as its own whole-turn task, so they
120+
* own the run they start and cannot take steering or a restart. */
121+
export function isNativeMaintenanceCommand(message: {
122+
readonly text: string;
123+
readonly attachments: ReadonlyArray<ChatAttachment>;
124+
}): boolean {
125+
return (
126+
message.attachments.length === 0 &&
127+
["/compact", "/logout"].includes(message.text.trim().toLowerCase())
128+
);
129+
}
130+
131+
/** Compaction and sign-out runs cannot take a steer or a restart, so a message
132+
* sent while one runs still belongs after it rather than as a rejected send. */
133+
function queueBehindMaintenanceRun(
134+
projection: OrchestrationV2ThreadProjection,
135+
decision: MessageDispatchMode,
136+
): MessageDispatchMode {
137+
if (decision.type !== "steer_active" && decision.type !== "restart_active") return decision;
138+
const targetRun = projection.runs.find((run) => run.id === decision.targetRunId);
139+
const targetMessage =
140+
targetRun === undefined
141+
? undefined
142+
: projection.messages?.find((message) => message.id === targetRun.userMessageId);
143+
return targetMessage !== undefined && isNativeMaintenanceCommand(targetMessage)
144+
? { type: "queue_after_active" }
145+
: decision;
146+
}
147+
118148
/** Resolve client intent from the state serialized by the thread dispatch lock. */
119149
export function resolveMessageDispatchIntent(
120150
projection: OrchestrationV2ThreadProjection,
121151
requestedMode: MessageDispatchMode,
122152
deliveryIntent?: "auto" | "steer" | "restart",
123153
): MessageDispatchMode {
124-
if (deliveryIntent === undefined) return requestedMode;
154+
if (deliveryIntent === undefined) {
155+
return queueBehindMaintenanceRun(projection, requestedMode);
156+
}
125157

126158
const activeRun = projection.runs.findLast(
127159
(run) =>
@@ -132,10 +164,16 @@ export function resolveMessageDispatchIntent(
132164
);
133165
if (activeRun === undefined) return { type: "start_immediately" };
134166
if (deliveryIntent === "steer") {
135-
return { type: "steer_active", targetRunId: activeRun.id };
167+
return queueBehindMaintenanceRun(projection, {
168+
type: "steer_active",
169+
targetRunId: activeRun.id,
170+
});
136171
}
137172
if (deliveryIntent === "restart") {
138-
return { type: "restart_active", targetRunId: activeRun.id };
173+
return queueBehindMaintenanceRun(projection, {
174+
type: "restart_active",
175+
targetRunId: activeRun.id,
176+
});
139177
}
140178
if (activeRun.status === "preparing" || activeRun.status === "starting") {
141179
return { type: "queue_after_active" };
@@ -152,13 +190,19 @@ export function resolveMessageDispatchIntent(
152190
);
153191
const capabilities = providerSession?.capabilities.turns;
154192
if (capabilities?.supportsActiveSteering === true) {
155-
return { type: "steer_active", targetRunId: activeRun.id };
193+
return queueBehindMaintenanceRun(projection, {
194+
type: "steer_active",
195+
targetRunId: activeRun.id,
196+
});
156197
}
157198
if (capabilities?.supportsQueuedMessages === true) {
158199
return { type: "queue_after_active" };
159200
}
160201
if (capabilities?.supportsSteeringByInterruptRestart === true) {
161-
return { type: "restart_active", targetRunId: activeRun.id };
202+
return queueBehindMaintenanceRun(projection, {
203+
type: "restart_active",
204+
targetRunId: activeRun.id,
205+
});
162206
}
163207
return { type: "queue_after_active" };
164208
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ import {
5656
SHARED_WORKSPACE_RESTORE_MESSAGE,
5757
} from "./CheckpointRestoreSafety.ts";
5858
import { CheckpointServiceV2 } from "./CheckpointService.ts";
59-
import { CommandPolicyV2, resolveMessageDispatchIntent } from "./CommandPolicy.ts";
59+
import {
60+
CommandPolicyV2,
61+
isNativeMaintenanceCommand,
62+
resolveMessageDispatchIntent,
63+
} from "./CommandPolicy.ts";
6064
import { CommandReceiptStoreV2 } from "./CommandReceiptStore.ts";
6165
import { ContextHandoffServiceV2 } from "./ContextHandoffService.ts";
6266
import { notificationTurnItem } from "./Notification.ts";
@@ -262,17 +266,6 @@ function nextRunOrdinal(projection: OrchestrationV2ThreadProjection): number {
262266
return projection.runs.length + 1;
263267
}
264268

265-
function isNativeMaintenanceCommand(message: {
266-
readonly text: string;
267-
readonly attachments: ReadonlyArray<ChatAttachment>;
268-
readonly context?: import("@t3tools/contracts").OrchestrationMessageContext | undefined;
269-
}): boolean {
270-
return (
271-
message.attachments.length === 0 &&
272-
["/compact", "/logout"].includes(message.text.trim().toLowerCase())
273-
);
274-
}
275-
276269
const threadPullRequestLinksEqual = Schema.toEquivalence(Schema.NullOr(ThreadLinkedPullRequest));
277270

278271
function commandThreadId(command: OrchestrationV2Command): ThreadId {

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10598,6 +10598,7 @@ export default function ChatView(props: ChatViewProps) {
1059810598
forceExpandedOnMobile={forceExpandedMobileComposer && isDraftHeroState}
1059910599
projectSelectionRequired={isLocalDraftThread && activeProject === null}
1060010600
phase={phase}
10601+
activeTurnIsCompaction={isCompacting}
1060110602
isConnecting={isConnecting}
1060210603
isSendBusy={isSendBusy || isSavingQueuedEdit}
1060310604
isRevertingCheckpoint={isRevertingCheckpoint}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
13151315
isComplete: boolean;
13161316
} | null;
13171317
isRunning: boolean;
1318+
activeTurnIsCompaction: boolean;
13181319
followUpBehavior: "queue" | "steer";
13191320
alternateShortcutLabel: string | null;
13201321
showPlanFollowUpPrompt: boolean;
@@ -1351,6 +1352,7 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
13511352
compact={props.compact}
13521353
pendingAction={props.pendingAction}
13531354
isRunning={props.isRunning}
1355+
activeTurnIsCompaction={props.activeTurnIsCompaction}
13541356
followUpBehavior={props.followUpBehavior}
13551357
alternateShortcutLabel={props.alternateShortcutLabel}
13561358
showPlanFollowUpPrompt={props.showPlanFollowUpPrompt}
@@ -1474,6 +1476,8 @@ export interface ChatComposerProps {
14741476

14751477
// Session phase
14761478
phase: SessionPhase;
1479+
/** The in-flight turn is context compaction, so follow-ups queue behind it. */
1480+
activeTurnIsCompaction: boolean;
14771481
isConnecting: boolean;
14781482
isSendBusy: boolean;
14791483
isRevertingCheckpoint?: boolean;
@@ -1646,6 +1650,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
16461650
forceExpandedOnMobile,
16471651
projectSelectionRequired,
16481652
phase,
1653+
activeTurnIsCompaction,
16491654
isConnecting,
16501655
isSendBusy,
16511656
isRevertingCheckpoint = false,
@@ -4078,6 +4083,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
40784083
running: phase === "running",
40794084
alternateModifier: false,
40804085
activeTurnDefault: settings.followUpBehavior,
4086+
activeTurnIsCompaction,
40814087
}),
40824088
submissionIntent,
40834089
);
@@ -4099,6 +4105,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
40994105
noProviderAvailable,
41004106
onSend,
41014107
settings.followUpBehavior,
4108+
activeTurnIsCompaction,
41024109
phase,
41034110
promptRef,
41044111
shouldBlurMobileComposerOnSubmit,
@@ -4113,10 +4120,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
41134120
running: phase === "running",
41144121
alternateModifier: event.metaKey || event.ctrlKey,
41154122
activeTurnDefault: settings.followUpBehavior,
4123+
activeTurnIsCompaction,
41164124
}),
41174125
);
41184126
},
4119-
[phase, settings.followUpBehavior, submitComposer],
4127+
[phase, activeTurnIsCompaction, settings.followUpBehavior, submitComposer],
41204128
);
41214129
const submitCitationAndSend = useCallback(() => {
41224130
submitComposer(
@@ -4125,9 +4133,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
41254133
running: phase === "running",
41264134
alternateModifier: false,
41274135
activeTurnDefault: settings.followUpBehavior,
4136+
activeTurnIsCompaction,
41284137
}),
41294138
);
4130-
}, [phase, settings.followUpBehavior, submitComposer]);
4139+
}, [phase, activeTurnIsCompaction, settings.followUpBehavior, submitComposer]);
41314140
const compactThreadContext = useCallback(() => {
41324141
if (
41334142
compactDisabled ||
@@ -4301,6 +4310,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43014310
running: phase === "running",
43024311
alternateModifier: submissionIntent === "alternate",
43034312
activeTurnDefault: settings.followUpBehavior,
4313+
activeTurnIsCompaction,
43044314
}),
43054315
submissionIntent,
43064316
);
@@ -7386,6 +7396,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
73867396
activeThreadModelDisplayName={activeThreadModelDisplayName}
73877397
pendingAction={pendingPrimaryAction}
73887398
isRunning={phase === "running"}
7399+
activeTurnIsCompaction={activeTurnIsCompaction}
73897400
followUpBehavior={settings.followUpBehavior}
73907401
alternateShortcutLabel={shortcutLabelForCommand(
73917402
keybindings,

‎apps/web/src/components/chat/ComposerPrimaryActions.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface ComposerPrimaryActionsProps {
3232
compact: boolean;
3333
pendingAction: PendingActionState | null;
3434
isRunning: boolean;
35+
activeTurnIsCompaction?: boolean;
3536
followUpBehavior?: "queue" | "steer";
3637
alternateShortcutLabel?: string | null;
3738
showPlanFollowUpPrompt: boolean;
@@ -76,6 +77,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
7677
compact,
7778
pendingAction,
7879
isRunning,
80+
activeTurnIsCompaction = false,
7981
followUpBehavior = "steer",
8082
alternateShortcutLabel = null,
8183
showPlanFollowUpPrompt,
@@ -103,9 +105,18 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
103105
resolveComposerDispatchMode({
104106
running: isRunning,
105107
activeTurnDefault: followUpBehavior,
108+
activeTurnIsCompaction,
106109
alternateModifier: shortcutModifiers.metaKey || shortcutModifiers.ctrlKey,
107110
}) === "queue";
108-
const alternateAction = alternateComposerDispatchAction(followUpBehavior);
111+
const alternateAction = alternateComposerDispatchAction(followUpBehavior, activeTurnIsCompaction);
112+
// What a plain click delivers while the turn runs: the configured default,
113+
// except compaction runs, which queue every follow-up.
114+
const effectiveDefaultAction = resolveComposerDispatchMode({
115+
running: isRunning,
116+
activeTurnDefault: followUpBehavior,
117+
activeTurnIsCompaction,
118+
alternateModifier: false,
119+
});
109120
const isSendDisabled = sendDisabledReason !== null;
110121
const stageBackdropVariant = useSidebarStageBackdropVariant(
111122
environmentIdentificationMode === "artwork",
@@ -273,7 +284,9 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
273284
const submitTooltip =
274285
submitStatus ??
275286
(isRunning && !isEditingQueuedMessage
276-
? `Click to ${followUpBehavior}, Ctrl/⌘-click${alternateShortcutLabel ? ` or ${alternateShortcutLabel}` : ""} to ${alternateAction}`
287+
? effectiveDefaultAction === alternateAction
288+
? `Click to ${effectiveDefaultAction}`
289+
: `Click to ${effectiveDefaultAction}, Ctrl/⌘-click${alternateShortcutLabel ? ` or ${alternateShortcutLabel}` : ""} to ${alternateAction}`
277290
: submitLabel);
278291

279292
const sendButton = (

‎packages/client-runtime/src/state/composerDispatch.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,33 @@ describe("resolveComposerDispatchMode", () => {
6969
expect(alternateComposerDispatchAction("steer")).toBe("queue");
7070
expect(alternateComposerDispatchAction()).toBe("queue");
7171
});
72+
73+
it("queues every follow-up while the active turn is compacting", () => {
74+
expect(
75+
resolveComposerDispatchMode({
76+
running: true,
77+
alternateModifier: false,
78+
activeTurnIsCompaction: true,
79+
}),
80+
).toBe("queue");
81+
expect(
82+
resolveComposerDispatchMode({
83+
running: true,
84+
alternateModifier: true,
85+
activeTurnDefault: "steer",
86+
activeTurnIsCompaction: true,
87+
}),
88+
).toBe("queue");
89+
expect(
90+
resolveComposerDispatchMode({
91+
running: false,
92+
alternateModifier: false,
93+
activeTurnIsCompaction: true,
94+
}),
95+
).toBe("auto");
96+
});
97+
98+
it("labels the alternate as queue while compacting", () => {
99+
expect(alternateComposerDispatchAction("steer", true)).toBe("queue");
100+
});
72101
});

‎packages/client-runtime/src/state/composerDispatch.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ export function resolveComposerDispatchMode(input: {
1313
readonly running: boolean;
1414
readonly alternateModifier: boolean;
1515
readonly activeTurnDefault?: ActiveTurnComposerAction;
16+
/** The in-flight turn is context compaction, which cannot take a steer, so
17+
* every follow-up delivery queues behind it. */
18+
readonly activeTurnIsCompaction?: boolean;
1619
}): ComposerDispatchMode {
1720
if (!input.running) return "auto";
21+
if (input.activeTurnIsCompaction) return "queue";
1822
const defaultAction = input.activeTurnDefault ?? "steer";
1923
if (input.alternateModifier) return defaultAction === "queue" ? "steer" : "queue";
2024
return defaultAction;
@@ -23,10 +27,12 @@ export function resolveComposerDispatchMode(input: {
2327
/** What the alternate would do, for labelling the affordance that triggers it. */
2428
export function alternateComposerDispatchAction(
2529
activeTurnDefault?: ActiveTurnComposerAction,
30+
activeTurnIsCompaction?: boolean,
2631
): ActiveTurnComposerAction {
2732
return resolveComposerDispatchMode({
2833
running: true,
2934
alternateModifier: true,
3035
...(activeTurnDefault === undefined ? {} : { activeTurnDefault }),
36+
...(activeTurnIsCompaction === undefined ? {} : { activeTurnIsCompaction }),
3137
}) as ActiveTurnComposerAction;
3238
}

0 commit comments

Comments
 (0)