Skip to content

Commit b3f5d52

Browse files
NikitaMGrimmmaria-rcksjuliusmarminge
authored
fix(composer): steer instead of queueing during automatic compaction (pingdotgg#24138)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 6700cf6 commit b3f5d52

9 files changed

Lines changed: 132 additions & 27 deletions

File tree

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useAtomValue } from "@effect/atom-react";
66
import { shouldCompactBeforeSend } from "@t3tools/client-runtime/state/context-window";
77
import { threadRuntimeIsActive } from "@t3tools/client-runtime/state/shell";
88
import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints";
9+
import { threadSteersDuringCompaction } from "@t3tools/client-runtime/state/thread-workflows";
910
import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff";
1011
import {
1112
deriveProviderSubagentStatus,
@@ -84,6 +85,7 @@ import {
8485
} from "./use-composer-drafts";
8586
import { isClearComposerCommand, parseSideChatCommand } from "@t3tools/shared/composerTrigger";
8687
import {
88+
compactionBlocksSteering,
8789
resolveComposerDispatchMode,
8890
type ActiveTurnComposerAction,
8991
} from "@t3tools/client-runtime/state/composer-dispatch";
@@ -322,24 +324,19 @@ export function useThreadComposerState(input: {
322324
? deriveThreadActivityRun(selectedThreadProjection.projection)
323325
: (selectedThreadShell?.latestRun ?? null);
324326

325-
const isCompacting = (() => {
327+
const activeRunId =
328+
selectedThreadRuntime?.activeRunId && threadRuntimeIsActive(selectedThreadRuntime)
329+
? selectedThreadRuntime.activeRunId
330+
: null;
331+
const compactCommandIsActive = (() => {
326332
const queuedCompact = selectedThreadQueuedMessages.some(
327333
(message) =>
328334
message.messageId === dispatchingQueuedMessageId &&
329335
message.text.trim().toLowerCase() === "/compact" &&
330336
message.attachments.length === 0,
331337
);
332338
if (queuedCompact) return true;
333-
const activeRunId = selectedThreadRuntime?.activeRunId;
334-
if (!activeRunId || !threadRuntimeIsActive(selectedThreadRuntime)) return false;
335-
if (
336-
selectedThreadVisibleTurnItems.some(
337-
({ item }) =>
338-
item.runId === activeRunId && item.type === "compaction" && item.status === "running",
339-
)
340-
) {
341-
return true;
342-
}
339+
if (!activeRunId) return false;
343340
const compactMessage = selectedThreadVisibleTurnItems.findLast(
344341
({ item }) =>
345342
item.runId === activeRunId &&
@@ -358,8 +355,21 @@ export function useThreadComposerState(input: {
358355
item.status === "cancelled"),
359356
);
360357
})();
361-
// A compacting turn cannot take steering input, so follow-ups queue meanwhile.
362-
const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true && !isCompacting;
358+
const compactionRunning =
359+
activeRunId !== null &&
360+
selectedThreadVisibleTurnItems.some(
361+
({ item }) =>
362+
item.runId === activeRunId && item.type === "compaction" && item.status === "running",
363+
);
364+
const isCompacting = compactCommandIsActive || compactionRunning;
365+
const compactionBlocksSteer = compactionBlocksSteering({
366+
compactCommandIsActive,
367+
compactionRunning,
368+
steersDuringCompaction: threadSteersDuringCompaction(selectedThreadProjection?.projection),
369+
run: selectedThreadProjection?.projection.runs.find((run) => run.id === activeRunId),
370+
messages: selectedThreadMessages,
371+
});
372+
const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true && !compactionBlocksSteer;
363373
// A stop or a restart holds the queue, completion wakes included, until it
364374
// is resumed; the queue sheet lists only the user's own messages.
365375
const queueHeld = queueWorkflow?.isHeld === true;

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export const CodexProviderCapabilitiesV2 = {
265265
emitsTurnCompleted: true,
266266
supportsInterrupt: true,
267267
supportsActiveSteering: true,
268+
activeSteeringDuringCompaction: true,
268269
supportsSteeringByInterruptRestart: true,
269270
supportsQueuedMessages: true,
270271
terminalStatusQuality: "strong",

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ import {
9090
} from "@t3tools/client-runtime/state/subagentRuntime";
9191
import {
9292
getUserQueuedThreadRuns,
93+
threadSteersDuringCompaction,
9394
threadSupportsProviderHandoff,
9495
} from "@t3tools/client-runtime/state/thread-workflows";
96+
import { compactionBlocksSteering } from "@t3tools/client-runtime/state/composer-dispatch";
9597
import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff";
9698
import { HandoffRow } from "./chat/V2LifecycleRow";
9799
import {
@@ -1419,9 +1421,17 @@ export default function ChatView(props: ChatViewProps) {
14191421
item.runId === activeRuntime?.activeRunId &&
14201422
item.status === "running",
14211423
);
1424+
const compactCommandIsActive = compactRequestIsActive && !compactionSettled;
14221425
const isCompacting =
14231426
(isSendBusy || phase === "connecting" || phase === "running") &&
1424-
(compactionRunning || (compactRequestIsActive && !compactionSettled));
1427+
(compactionRunning || compactCommandIsActive);
1428+
const compactionBlocksSteer = compactionBlocksSteering({
1429+
compactCommandIsActive,
1430+
compactionRunning,
1431+
steersDuringCompaction: threadSteersDuringCompaction(serverProjection),
1432+
run: serverProjection?.runs.find((run) => run.id === activeRuntime?.activeRunId),
1433+
messages: serverProjection?.messages,
1434+
});
14251435
const isWorking =
14261436
phase === "running" || isSendBusy || isCompacting || runlessWorkStartedAt !== null;
14271437
const activeContextWindow = deriveLatestContextWindowSnapshot(
@@ -3576,7 +3586,7 @@ export default function ChatView(props: ChatViewProps) {
35763586
isLocalDraftThread && activeProject === null
35773587
}
35783588
phase={phase}
3579-
isCompacting={isCompacting}
3589+
canSteer={!compactionBlocksSteer}
35803590
canInterrupt={canInterruptRunningThread}
35813591
isSendBusy={
35823592
(isSendBusy && !sendQueuesBehindSetup) ||

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export interface ChatComposerProps {
370370
isRevertingCheckpoint?: boolean;
371371
sendDisabledReason: string | null;
372372
isPreparingWorktree: boolean;
373-
isCompacting: boolean;
373+
canSteer: boolean;
374374
bannerItems: readonly ComposerBannerStackItem[];
375375
resumeCompactionTokens: number | null;
376376
keepFullHistory: boolean;
@@ -516,7 +516,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
516516
isRevertingCheckpoint = false,
517517
sendDisabledReason: externalSendDisabledReason,
518518
isPreparingWorktree,
519-
isCompacting,
519+
canSteer,
520520
environmentUnavailable,
521521
activePendingApproval: incomingPendingApproval,
522522
pendingApprovals,
@@ -2144,7 +2144,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
21442144
running: phase === "running",
21452145
alternateModifier: false,
21462146
activeTurnDefault: followUpBehavior,
2147-
canSteer: !isCompacting,
2147+
canSteer,
21482148
}),
21492149
submissionIntent,
21502150
);
@@ -2185,7 +2185,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
21852185
resolveComposerDispatchMode({
21862186
running: phase === "running",
21872187
alternateModifier: intent === "alternate",
2188-
canSteer: !isCompacting,
2188+
canSteer,
21892189
activeTurnDefault: followUpBehavior,
21902190
}),
21912191
intent,
@@ -2200,7 +2200,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22002200
running: phase === "running",
22012201
alternateModifier: event.metaKey || event.ctrlKey,
22022202
activeTurnDefault: followUpBehavior,
2203-
canSteer: !isCompacting,
2203+
canSteer,
22042204
}),
22052205
);
22062206
};
@@ -2212,7 +2212,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22122212
running: phase === "running",
22132213
alternateModifier: false,
22142214
activeTurnDefault: followUpBehavior,
2215-
canSteer: !isCompacting,
2215+
canSteer,
22162216
}),
22172217
);
22182218
};
@@ -2293,7 +2293,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22932293
sendShortcut: settings.sendShortcut,
22942294
routeKind,
22952295
phase,
2296-
isCompacting,
2296+
canSteer,
22972297
followUpBehavior,
22982298
isSoftwareKeyboardOpen,
22992299
isLiteralPendingAnswer,
@@ -3584,7 +3584,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
35843584
isRunning={phase === "running"}
35853585
canInterrupt={canInterrupt}
35863586
followUpBehavior={followUpBehavior}
3587-
canSteer={!isCompacting}
3587+
canSteer={canSteer}
35883588
alternateShortcutLabel={shortcutLabelForCommand(
35893589
keybindings,
35903590
"composer.sendAlternate",

‎apps/web/src/components/chat/composerCommandKey.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function composerCommandKeyHandler(input: {
3838
readonly sendShortcut: ChatComposerProps["settings"]["sendShortcut"];
3939
readonly routeKind: ChatComposerProps["routeKind"];
4040
readonly phase: ChatComposerProps["phase"];
41-
readonly isCompacting: boolean;
41+
readonly canSteer: boolean;
4242
readonly followUpBehavior: ChatComposerProps["settings"]["followUpBehavior"];
4343
readonly isSoftwareKeyboardOpen: boolean;
4444
readonly isLiteralPendingAnswer: boolean;
@@ -91,7 +91,7 @@ export function composerCommandKeyHandler(input: {
9191
sendShortcut,
9292
routeKind,
9393
phase,
94-
isCompacting,
94+
canSteer,
9595
followUpBehavior,
9696
isSoftwareKeyboardOpen,
9797
isLiteralPendingAnswer,
@@ -267,7 +267,7 @@ export function composerCommandKeyHandler(input: {
267267
running: phase === "running",
268268
alternateModifier: submissionIntent === "alternate",
269269
activeTurnDefault: followUpBehavior,
270-
canSteer: !isCompacting,
270+
canSteer,
271271
}),
272272
submissionIntent,
273273
);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expect, it } from "vite-plus/test";
2+
3+
import { compactionBlocksSteering } from "./composerDispatch.ts";
4+
5+
it("blocks steering for explicit compaction, and for automatic compaction unless the provider takes steers then", () => {
6+
const during = (
7+
text: string | undefined,
8+
attachments: ReadonlyArray<unknown> = [],
9+
steersDuringCompaction = true,
10+
) =>
11+
compactionBlocksSteering({
12+
compactCommandIsActive: false,
13+
compactionRunning: true,
14+
steersDuringCompaction,
15+
run: { userMessageId: "user" },
16+
messages: [
17+
...(text === undefined ? [] : [{ id: "user", text, attachments }]),
18+
{ id: "later", text: "/compact" },
19+
],
20+
});
21+
22+
for (const text of ["/compact", " /COMPACT ", "/compact keep the debugging notes"]) {
23+
expect(during(text)).toBe(true);
24+
}
25+
for (const text of ["Read the code", "/compactify", "please /compact", undefined]) {
26+
expect(during(text)).toBe(false);
27+
}
28+
expect(during("/compact", [{}])).toBe(false);
29+
expect(during("Read the code", [], false)).toBe(true);
30+
31+
expect(
32+
compactionBlocksSteering({
33+
compactCommandIsActive: true,
34+
compactionRunning: false,
35+
steersDuringCompaction: true,
36+
run: undefined,
37+
messages: undefined,
38+
}),
39+
).toBe(true);
40+
expect(
41+
compactionBlocksSteering({
42+
compactCommandIsActive: false,
43+
compactionRunning: false,
44+
steersDuringCompaction: false,
45+
run: { userMessageId: "user" },
46+
messages: [{ id: "user", text: "/compact" }],
47+
}),
48+
).toBe(false);
49+
});

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function resolveComposerDispatchMode(input: {
55
readonly running: boolean;
66
readonly alternateModifier: boolean;
77
readonly activeTurnDefault?: ActiveTurnComposerAction;
8-
/** False while the active turn cannot take steering input, such as during compaction. */
8+
/** False while the active turn cannot take steering input, such as an explicit /compact. */
99
readonly canSteer?: boolean;
1010
}): ComposerDispatchMode {
1111
if (!input.running) return "auto";
@@ -15,6 +15,31 @@ export function resolveComposerDispatchMode(input: {
1515
return defaultAction;
1616
}
1717

18+
// Automatic compaction takes steers only where the provider says a steer leaves it running.
19+
export function compactionBlocksSteering(input: {
20+
readonly compactCommandIsActive: boolean;
21+
readonly compactionRunning: boolean;
22+
readonly steersDuringCompaction: boolean;
23+
readonly run: { readonly userMessageId: string } | undefined;
24+
readonly messages:
25+
| ReadonlyArray<{
26+
readonly id: string;
27+
readonly text: string;
28+
readonly attachments?: ReadonlyArray<unknown>;
29+
}>
30+
| undefined;
31+
}): boolean {
32+
if (input.compactCommandIsActive) return true;
33+
if (!input.compactionRunning) return false;
34+
if (!input.steersDuringCompaction) return true;
35+
if (input.run === undefined) return false;
36+
const message = input.messages?.find((message) => message.id === input.run?.userMessageId);
37+
return (
38+
(message?.attachments?.length ?? 0) === 0 &&
39+
/^\/compact(?:\s|$)/i.test(message?.text.trim() ?? "")
40+
);
41+
}
42+
1843
export function alternateComposerDispatchAction(
1944
activeTurnDefault?: ActiveTurnComposerAction,
2045
): ActiveTurnComposerAction {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ function resolveThreadProviderSession(projection: Projection): ProviderSession |
9292
);
9393
}
9494

95+
export function threadSteersDuringCompaction(projection: Projection | null | undefined): boolean {
96+
if (projection == null) return false;
97+
return (
98+
resolveThreadProviderSession(projection)?.capabilities.turns.activeSteeringDuringCompaction ===
99+
true
100+
);
101+
}
102+
95103
export function threadSupportsProviderHandoff(projection: Projection | null | undefined): boolean {
96104
if (projection == null) return false;
97105
const session = resolveThreadProviderSession(projection);

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ export const OrchestrationV2TurnCapabilities = Schema.Struct({
288288
activeSteeringInterruptsTools: Schema.optional(Schema.Boolean),
289289
// Some take text only, so a steer with attachments restarts the turn instead.
290290
activeSteeringAcceptsAttachments: Schema.optional(Schema.Boolean),
291+
// Only where a steer leaves a running automatic compaction intact; otherwise clients queue.
292+
activeSteeringDuringCompaction: Schema.optional(Schema.Boolean),
291293
supportsSteeringByInterruptRestart: Schema.Boolean,
292294
supportsQueuedMessages: Schema.Boolean,
293295
terminalStatusQuality: Schema.Literals(["strong", "weak", "none"]),

0 commit comments

Comments
 (0)