Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions apps/mobile/src/state/use-thread-composer-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -322,24 +324,19 @@ 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 &&
message.text.trim().toLowerCase() === "/compact" &&
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 &&
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export const CodexProviderCapabilitiesV2 = {
emitsTurnCompleted: true,
supportsInterrupt: true,
supportsActiveSteering: true,
activeSteeringDuringCompaction: true,
supportsSteeringByInterruptRestart: true,
supportsQueuedMessages: true,
terminalStatusQuality: "strong",
Expand Down
14 changes: 12 additions & 2 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -3592,7 +3602,7 @@ export default function ChatView(props: ChatViewProps) {
isLocalDraftThread && activeProject === null
}
phase={phase}
isCompacting={isCompacting}
canSteer={!compactionBlocksSteer}
canInterrupt={canInterruptRunningThread}
isSendBusy={
(isSendBusy && !sendQueuesBehindSetup) ||
Expand Down
16 changes: 8 additions & 8 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -515,7 +515,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
isRevertingCheckpoint = false,
sendDisabledReason: externalSendDisabledReason,
isPreparingWorktree,
isCompacting,
canSteer,
environmentUnavailable,
activePendingApproval: incomingPendingApproval,
pendingApprovals,
Expand Down Expand Up @@ -2140,7 +2140,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
running: phase === "running",
alternateModifier: false,
activeTurnDefault: followUpBehavior,
canSteer: !isCompacting,
canSteer,
}),
submissionIntent,
);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}),
);
};
Expand All @@ -2208,7 +2208,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
running: phase === "running",
alternateModifier: false,
activeTurnDefault: followUpBehavior,
canSteer: !isCompacting,
canSteer,
}),
);
};
Expand Down Expand Up @@ -2289,7 +2289,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
sendShortcut: settings.sendShortcut,
routeKind,
phase,
isCompacting,
canSteer,
followUpBehavior,
isSoftwareKeyboardOpen,
isLiteralPendingAnswer,
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/components/chat/composerCommandKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,7 +91,7 @@ export function composerCommandKeyHandler(input: {
sendShortcut,
routeKind,
phase,
isCompacting,
canSteer,
followUpBehavior,
isSoftwareKeyboardOpen,
isLiteralPendingAnswer,
Expand Down Expand Up @@ -267,7 +267,7 @@ export function composerCommandKeyHandler(input: {
running: phase === "running",
alternateModifier: submissionIntent === "alternate",
activeTurnDefault: followUpBehavior,
canSteer: !isCompacting,
canSteer,
}),
submissionIntent,
);
Expand Down
49 changes: 49 additions & 0 deletions packages/client-runtime/src/state/composerDispatch.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> = [],
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);
});
27 changes: 26 additions & 1 deletion packages/client-runtime/src/state/composerDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<unknown>;
}>
| 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 {
Expand Down
8 changes: 8 additions & 0 deletions packages/client-runtime/src/state/threadWorkflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
Loading