Skip to content

Commit 4ddc3e3

Browse files
committed
fix(mobile): stop background work after the turn settles
1 parent 611132c commit 4ddc3e3

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ export interface ThreadDetailScreenProps {
214214
readonly onNativePasteText: (paste: ComposerTextPaste) => Promise<void>;
215215
readonly onRemoveDraftImage: (imageId: string) => void;
216216
readonly onStopThread: () => void;
217+
readonly stoppingBackgroundWork: boolean;
218+
readonly onStopBackgroundWork: () => void;
217219
readonly onSendMessage: (followUp?: ActiveTurnComposerAction) => Promise<MessageId | null>;
218220
readonly onReconnectEnvironment: () => void;
219221
/** Whether the model picker may offer providers other than this thread's. */
@@ -1187,6 +1189,14 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
11871189
<FloatingWorkingControl
11881190
colorScheme={isDarkMode ? "dark" : "light"}
11891191
status={floatingStatus}
1192+
backgroundStop={
1193+
props.canOperateThread
1194+
? {
1195+
stopping: props.stoppingBackgroundWork,
1196+
onPress: props.onStopBackgroundWork,
1197+
}
1198+
: null
1199+
}
11901200
lift={floatingControlLift}
11911201
devicePreview={
11921202
devicePreviews.length > 0

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,15 @@ function ThreadRouteContent(
359359
const gitActions = useSelectedThreadGitActions();
360360
const requests = useSelectedThreadRequests();
361361
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, "thread interrupt");
362+
const [backgroundStopRequest, setBackgroundStopRequest] = useState<{
363+
threadKey: string;
364+
} | null>(null);
365+
const backgroundThreadKey = selectedThread
366+
? scopedThreadKey(selectedThread.environmentId, selectedThread.id)
367+
: null;
368+
if (backgroundStopRequest !== null && backgroundStopRequest.threadKey !== backgroundThreadKey) {
369+
setBackgroundStopRequest(null);
370+
}
362371
const loadEarlierHistory = useAtomCommand(threadEnvironment.loadEarlierHistory, {
363372
label: "load earlier thread history",
364373
reportFailure: false,
@@ -707,6 +716,26 @@ function ThreadRouteContent(
707716
});
708717
}, [composer.interruptibleRunId, interruptThreadTurn, selectedThread]);
709718

719+
const handleStopBackgroundWork = useCallback(async () => {
720+
if (
721+
!selectedThread ||
722+
!readEnvironmentScope(selectedThread.environmentId, AuthOrchestrationOperateScope)
723+
) {
724+
return;
725+
}
726+
const request = {
727+
threadKey: scopedThreadKey(selectedThread.environmentId, selectedThread.id),
728+
};
729+
setBackgroundStopRequest(request);
730+
await interruptThreadTurn({
731+
environmentId: selectedThread.environmentId,
732+
input: { threadId: selectedThread.id },
733+
});
734+
// Acceptance does not confirm termination. Allow retry while tasks remain,
735+
// including when the interrupt fails, matching the web client's behavior.
736+
setBackgroundStopRequest((current) => (current === request ? null : current));
737+
}, [interruptThreadTurn, selectedThread]);
738+
710739
const handleOpenTerminal = useCallback(
711740
(nextTerminalId?: string | null) => {
712741
terminalDebugLog("terminal-menu:open-existing", {
@@ -1118,6 +1147,8 @@ function ThreadRouteContent(
11181147
onRemoveDraftImage={composer.onRemoveDraftImage}
11191148
serverConfig={serverConfig}
11201149
onStopThread={awaitingBootstrapTurn ? handleCancelWorktreeSetup : handleStopThread}
1150+
stoppingBackgroundWork={backgroundStopRequest !== null}
1151+
onStopBackgroundWork={handleStopBackgroundWork}
11211152
onSendMessage={composer.onSendMessage}
11221153
onReconnectEnvironment={handleReconnectEnvironment}
11231154
canSwitchThreadProvider={composer.canSwitchThreadProvider}

‎apps/mobile/src/features/threads/floating-working-control.tsx‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTRO
6767
export function FloatingWorkingControl(props: {
6868
readonly colorScheme: "light" | "dark";
6969
readonly status: FloatingWorkingStatus | null;
70+
/** Stop for background work that outlived its turn, beside the label naming it. */
71+
readonly backgroundStop: { readonly stopping: boolean; readonly onPress: () => void } | null;
7072
readonly devicePreview: { readonly count: number; readonly onPress: () => void } | null;
7173
readonly browserPreview: { readonly count: number; readonly onPress: () => void } | null;
7274
readonly showScrollToEnd: boolean;
@@ -82,6 +84,9 @@ export function FloatingWorkingControl(props: {
8284
const [overlayWidth, setOverlayWidth] = useState(windowWidth);
8385
const [queueWidth, setQueueWidth] = useState(0);
8486
const [agentsWidth, setAgentsWidth] = useState(0);
87+
const [stopWidth, setStopWidth] = useState(0);
88+
const backgroundStop = props.status?.kind === "background" ? props.backgroundStop : null;
89+
const hasStop = backgroundStop !== null;
8590
const hasQueue = props.queuedCount > 0;
8691
const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
8792
const [previewWidth, setPreviewWidth] = useState(0);
@@ -94,6 +99,7 @@ export function FloatingWorkingControl(props: {
9499
Math.min(overlayWidth, windowWidth) -
95100
CONTROL_HEIGHT -
96101
32 -
102+
(hasStop ? stopWidth : 0) -
97103
(hasQueue ? queueWidth : 0) -
98104
(hasAgents ? agentsWidth : 0) -
99105
(hasPreview ? previewWidth : 0),
@@ -159,9 +165,9 @@ export function FloatingWorkingControl(props: {
159165
hasAgents ||
160166
hasQueue ||
161167
(props.devicePreview !== null && props.browserPreview !== null);
162-
// The queue, agents, and reconnect labels have separate tap targets.
168+
// The stop, queue, agents, and reconnect labels have separate tap targets.
163169
const statusInteractive = props.status?.kind === "connection";
164-
const capsuleInteractive = statusInteractive || hasQueue || hasAgents || hasPreview;
170+
const capsuleInteractive = statusInteractive || hasStop || hasQueue || hasAgents || hasPreview;
165171
// The host stays centered on the capsule, but its measurement constraint
166172
// comes from the overlay, independent of the capsule's current width.
167173
const statusContent =
@@ -192,6 +198,21 @@ export function FloatingWorkingControl(props: {
192198
const capsuleContent = (
193199
<View className="flex-row items-center">
194200
{statusContent}
201+
{backgroundStop !== null ? (
202+
<Pressable
203+
accessibilityRole="button"
204+
accessibilityLabel={backgroundStop.stopping ? "Stopping" : "Stop background work"}
205+
disabled={backgroundStop.stopping}
206+
onPress={backgroundStop.onPress}
207+
onLayout={(event) => setStopWidth(event.nativeEvent.layout.width)}
208+
className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
209+
>
210+
<View className="mr-1 h-4 w-px bg-border" />
211+
<Text className="font-t3-medium text-xs text-danger-foreground" numberOfLines={1}>
212+
{backgroundStop.stopping ? "Stopping…" : "Stop"}
213+
</Text>
214+
</Pressable>
215+
) : null}
195216
{hasPreview ? (
196217
<View
197218
className="h-11 flex-row items-center"

0 commit comments

Comments
 (0)