diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 61a72f1af65dc..702aa53f90f0f 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -6,6 +6,7 @@ import { useAtomValue } from "@effect/atom-react"; import { shouldCompactBeforeSend } from "@t3tools/client-runtime/state/context-window"; import { threadRuntimeIsActive } from "@t3tools/client-runtime/state/shell"; import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints"; +import { threadSteersDuringCompaction } from "@t3tools/client-runtime/state/thread-workflows"; import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff"; import { deriveProviderSubagentStatus, @@ -84,6 +85,7 @@ import { } from "./use-composer-drafts"; import { isClearComposerCommand, parseSideChatCommand } from "@t3tools/shared/composerTrigger"; import { + compactionBlocksSteering, resolveComposerDispatchMode, type ActiveTurnComposerAction, } from "@t3tools/client-runtime/state/composer-dispatch"; @@ -322,7 +324,11 @@ export function useThreadComposerState(input: { ? deriveThreadActivityRun(selectedThreadProjection.projection) : (selectedThreadShell?.latestRun ?? null); - const isCompacting = (() => { + const activeRunId = + selectedThreadRuntime?.activeRunId && threadRuntimeIsActive(selectedThreadRuntime) + ? selectedThreadRuntime.activeRunId + : null; + const compactCommandIsActive = (() => { const queuedCompact = selectedThreadQueuedMessages.some( (message) => message.messageId === dispatchingQueuedMessageId && @@ -330,16 +336,7 @@ export function useThreadComposerState(input: { message.attachments.length === 0, ); if (queuedCompact) return true; - const activeRunId = selectedThreadRuntime?.activeRunId; - if (!activeRunId || !threadRuntimeIsActive(selectedThreadRuntime)) return false; - if ( - selectedThreadVisibleTurnItems.some( - ({ item }) => - item.runId === activeRunId && item.type === "compaction" && item.status === "running", - ) - ) { - return true; - } + if (!activeRunId) return false; const compactMessage = selectedThreadVisibleTurnItems.findLast( ({ item }) => item.runId === activeRunId && @@ -358,8 +355,21 @@ export function useThreadComposerState(input: { item.status === "cancelled"), ); })(); - // A compacting turn cannot take steering input, so follow-ups queue meanwhile. - const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true && !isCompacting; + const compactionRunning = + activeRunId !== null && + selectedThreadVisibleTurnItems.some( + ({ item }) => + item.runId === activeRunId && item.type === "compaction" && item.status === "running", + ); + const isCompacting = compactCommandIsActive || compactionRunning; + const compactionBlocksSteer = compactionBlocksSteering({ + compactCommandIsActive, + compactionRunning, + steersDuringCompaction: threadSteersDuringCompaction(selectedThreadProjection?.projection), + run: selectedThreadProjection?.projection.runs.find((run) => run.id === activeRunId), + messages: selectedThreadMessages, + }); + const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true && !compactionBlocksSteer; // A stop or a restart holds the queue, completion wakes included, until it // is resumed; the queue sheet lists only the user's own messages. const queueHeld = queueWorkflow?.isHeld === true; diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 6953636a8dcf5..4a5985f6c2ef5 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -265,6 +265,7 @@ export const CodexProviderCapabilitiesV2 = { emitsTurnCompleted: true, supportsInterrupt: true, supportsActiveSteering: true, + activeSteeringDuringCompaction: true, supportsSteeringByInterruptRestart: true, supportsQueuedMessages: true, terminalStatusQuality: "strong", diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index f4561188df0cc..15b8c91ac6950 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -90,8 +90,10 @@ import { } from "@t3tools/client-runtime/state/subagentRuntime"; import { getUserQueuedThreadRuns, + threadSteersDuringCompaction, threadSupportsProviderHandoff, } from "@t3tools/client-runtime/state/thread-workflows"; +import { compactionBlocksSteering } from "@t3tools/client-runtime/state/composer-dispatch"; import { resolvePendingHandoff } from "@t3tools/client-runtime/handoff"; import { HandoffRow } from "./chat/V2LifecycleRow"; import { @@ -1438,9 +1440,17 @@ export default function ChatView(props: ChatViewProps) { item.runId === activeRuntime?.activeRunId && item.status === "running", ); + const compactCommandIsActive = compactRequestIsActive && !compactionSettled; const isCompacting = (isSendBusy || phase === "connecting" || phase === "running") && - (compactionRunning || (compactRequestIsActive && !compactionSettled)); + (compactionRunning || compactCommandIsActive); + const compactionBlocksSteer = compactionBlocksSteering({ + compactCommandIsActive, + compactionRunning, + steersDuringCompaction: threadSteersDuringCompaction(serverProjection), + run: serverProjection?.runs.find((run) => run.id === activeRuntime?.activeRunId), + messages: serverProjection?.messages, + }); const isWorking = phase === "running" || isSendBusy || isCompacting || runlessWorkStartedAt !== null; const activeContextWindow = deriveLatestContextWindowSnapshot( @@ -3592,7 +3602,7 @@ export default function ChatView(props: ChatViewProps) { isLocalDraftThread && activeProject === null } phase={phase} - isCompacting={isCompacting} + canSteer={!compactionBlocksSteer} canInterrupt={canInterruptRunningThread} isSendBusy={ (isSendBusy && !sendQueuesBehindSetup) || diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index cba7c434b1bab..0587fe2a1d8fe 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -369,7 +369,7 @@ export interface ChatComposerProps { isRevertingCheckpoint?: boolean; sendDisabledReason: string | null; isPreparingWorktree: boolean; - isCompacting: boolean; + canSteer: boolean; bannerItems: readonly ComposerBannerStackItem[]; resumeCompactionTokens: number | null; keepFullHistory: boolean; @@ -515,7 +515,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) isRevertingCheckpoint = false, sendDisabledReason: externalSendDisabledReason, isPreparingWorktree, - isCompacting, + canSteer, environmentUnavailable, activePendingApproval: incomingPendingApproval, pendingApprovals, @@ -2140,7 +2140,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) running: phase === "running", alternateModifier: false, activeTurnDefault: followUpBehavior, - canSteer: !isCompacting, + canSteer, }), submissionIntent, ); @@ -2181,7 +2181,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) resolveComposerDispatchMode({ running: phase === "running", alternateModifier: intent === "alternate", - canSteer: !isCompacting, + canSteer, activeTurnDefault: followUpBehavior, }), intent, @@ -2196,7 +2196,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) running: phase === "running", alternateModifier: event.metaKey || event.ctrlKey, activeTurnDefault: followUpBehavior, - canSteer: !isCompacting, + canSteer, }), ); }; @@ -2208,7 +2208,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) running: phase === "running", alternateModifier: false, activeTurnDefault: followUpBehavior, - canSteer: !isCompacting, + canSteer, }), ); }; @@ -2289,7 +2289,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) sendShortcut: settings.sendShortcut, routeKind, phase, - isCompacting, + canSteer, followUpBehavior, isSoftwareKeyboardOpen, isLiteralPendingAnswer, @@ -3584,7 +3584,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) isRunning={phase === "running"} canInterrupt={canInterrupt} followUpBehavior={followUpBehavior} - canSteer={!isCompacting} + canSteer={canSteer} alternateShortcutLabel={shortcutLabelForCommand( keybindings, "composer.sendAlternate", diff --git a/apps/web/src/components/chat/composerCommandKey.ts b/apps/web/src/components/chat/composerCommandKey.ts index ac5bba3475542..beaab007028ae 100644 --- a/apps/web/src/components/chat/composerCommandKey.ts +++ b/apps/web/src/components/chat/composerCommandKey.ts @@ -38,7 +38,7 @@ export function composerCommandKeyHandler(input: { readonly sendShortcut: ChatComposerProps["settings"]["sendShortcut"]; readonly routeKind: ChatComposerProps["routeKind"]; readonly phase: ChatComposerProps["phase"]; - readonly isCompacting: boolean; + readonly canSteer: boolean; readonly followUpBehavior: ChatComposerProps["settings"]["followUpBehavior"]; readonly isSoftwareKeyboardOpen: boolean; readonly isLiteralPendingAnswer: boolean; @@ -91,7 +91,7 @@ export function composerCommandKeyHandler(input: { sendShortcut, routeKind, phase, - isCompacting, + canSteer, followUpBehavior, isSoftwareKeyboardOpen, isLiteralPendingAnswer, @@ -267,7 +267,7 @@ export function composerCommandKeyHandler(input: { running: phase === "running", alternateModifier: submissionIntent === "alternate", activeTurnDefault: followUpBehavior, - canSteer: !isCompacting, + canSteer, }), submissionIntent, ); diff --git a/packages/client-runtime/src/state/composerDispatch.test.ts b/packages/client-runtime/src/state/composerDispatch.test.ts new file mode 100644 index 0000000000000..936ae33111ef4 --- /dev/null +++ b/packages/client-runtime/src/state/composerDispatch.test.ts @@ -0,0 +1,49 @@ +import { expect, it } from "vite-plus/test"; + +import { compactionBlocksSteering } from "./composerDispatch.ts"; + +it("blocks steering for explicit compaction, and for automatic compaction unless the provider takes steers then", () => { + const during = ( + text: string | undefined, + attachments: ReadonlyArray = [], + steersDuringCompaction = true, + ) => + compactionBlocksSteering({ + compactCommandIsActive: false, + compactionRunning: true, + steersDuringCompaction, + run: { userMessageId: "user" }, + messages: [ + ...(text === undefined ? [] : [{ id: "user", text, attachments }]), + { id: "later", text: "/compact" }, + ], + }); + + for (const text of ["/compact", " /COMPACT ", "/compact keep the debugging notes"]) { + expect(during(text)).toBe(true); + } + for (const text of ["Read the code", "/compactify", "please /compact", undefined]) { + expect(during(text)).toBe(false); + } + expect(during("/compact", [{}])).toBe(false); + expect(during("Read the code", [], false)).toBe(true); + + expect( + compactionBlocksSteering({ + compactCommandIsActive: true, + compactionRunning: false, + steersDuringCompaction: true, + run: undefined, + messages: undefined, + }), + ).toBe(true); + expect( + compactionBlocksSteering({ + compactCommandIsActive: false, + compactionRunning: false, + steersDuringCompaction: false, + run: { userMessageId: "user" }, + messages: [{ id: "user", text: "/compact" }], + }), + ).toBe(false); +}); diff --git a/packages/client-runtime/src/state/composerDispatch.ts b/packages/client-runtime/src/state/composerDispatch.ts index 145b1ba508147..c24bb95628d97 100644 --- a/packages/client-runtime/src/state/composerDispatch.ts +++ b/packages/client-runtime/src/state/composerDispatch.ts @@ -5,7 +5,7 @@ export function resolveComposerDispatchMode(input: { readonly running: boolean; readonly alternateModifier: boolean; readonly activeTurnDefault?: ActiveTurnComposerAction; - /** False while the active turn cannot take steering input, such as during compaction. */ + /** False while the active turn cannot take steering input, such as an explicit /compact. */ readonly canSteer?: boolean; }): ComposerDispatchMode { if (!input.running) return "auto"; @@ -15,6 +15,31 @@ export function resolveComposerDispatchMode(input: { return defaultAction; } +// Automatic compaction takes steers only where the provider says a steer leaves it running. +export function compactionBlocksSteering(input: { + readonly compactCommandIsActive: boolean; + readonly compactionRunning: boolean; + readonly steersDuringCompaction: boolean; + readonly run: { readonly userMessageId: string } | undefined; + readonly messages: + | ReadonlyArray<{ + readonly id: string; + readonly text: string; + readonly attachments?: ReadonlyArray; + }> + | undefined; +}): boolean { + if (input.compactCommandIsActive) return true; + if (!input.compactionRunning) return false; + if (!input.steersDuringCompaction) return true; + if (input.run === undefined) return false; + const message = input.messages?.find((message) => message.id === input.run?.userMessageId); + return ( + (message?.attachments?.length ?? 0) === 0 && + /^\/compact(?:\s|$)/i.test(message?.text.trim() ?? "") + ); +} + export function alternateComposerDispatchAction( activeTurnDefault?: ActiveTurnComposerAction, ): ActiveTurnComposerAction { diff --git a/packages/client-runtime/src/state/threadWorkflows.ts b/packages/client-runtime/src/state/threadWorkflows.ts index 9e13b669a1da5..0aae5835eff03 100644 --- a/packages/client-runtime/src/state/threadWorkflows.ts +++ b/packages/client-runtime/src/state/threadWorkflows.ts @@ -92,6 +92,14 @@ function resolveThreadProviderSession(projection: Projection): ProviderSession | ); } +export function threadSteersDuringCompaction(projection: Projection | null | undefined): boolean { + if (projection == null) return false; + return ( + resolveThreadProviderSession(projection)?.capabilities.turns.activeSteeringDuringCompaction === + true + ); +} + export function threadSupportsProviderHandoff(projection: Projection | null | undefined): boolean { if (projection == null) return false; const session = resolveThreadProviderSession(projection); diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index b1fd02b3d3a07..d5033fa02f433 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -288,6 +288,8 @@ export const OrchestrationV2TurnCapabilities = Schema.Struct({ activeSteeringInterruptsTools: Schema.optional(Schema.Boolean), // Some take text only, so a steer with attachments restarts the turn instead. activeSteeringAcceptsAttachments: Schema.optional(Schema.Boolean), + // Only where a steer leaves a running automatic compaction intact; otherwise clients queue. + activeSteeringDuringCompaction: Schema.optional(Schema.Boolean), supportsSteeringByInterruptRestart: Schema.Boolean, supportsQueuedMessages: Schema.Boolean, terminalStatusQuality: Schema.Literals(["strong", "weak", "none"]),