Skip to content

Commit 98c205b

Browse files
committed
fix(mobile): stop background work after the turn settles
1 parent 61b9790 commit 98c205b

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
@@ -215,6 +215,8 @@ export interface ThreadDetailScreenProps {
215215
readonly onNativePasteText: (paste: ComposerTextPaste) => Promise<void>;
216216
readonly onRemoveDraftImage: (imageId: string) => void;
217217
readonly onStopThread: () => void;
218+
readonly stoppingBackgroundWork: boolean;
219+
readonly onStopBackgroundWork: () => void;
218220
readonly onSendMessage: (followUp?: ActiveTurnComposerAction) => Promise<MessageId | null>;
219221
readonly onReconnectEnvironment: () => void;
220222
/** Whether the model picker may offer providers other than this thread's. */
@@ -1192,6 +1194,14 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
11921194
<FloatingWorkingControl
11931195
colorScheme={isDarkMode ? "dark" : "light"}
11941196
status={floatingStatus}
1197+
backgroundStop={
1198+
props.canOperateThread
1199+
? {
1200+
stopping: props.stoppingBackgroundWork,
1201+
onPress: props.onStopBackgroundWork,
1202+
}
1203+
: null
1204+
}
11951205
lift={floatingControlLift}
11961206
devicePreview={
11971207
devicePreviews.length > 0

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ function ThreadRouteContent(
249249
const gitActions = useSelectedThreadGitActions();
250250
const requests = useSelectedThreadRequests();
251251
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, "thread interrupt");
252+
const [backgroundStopRequest, setBackgroundStopRequest] = useState<{
253+
threadKey: string;
254+
} | null>(null);
255+
const backgroundThreadKey = selectedThread
256+
? scopedThreadKey(selectedThread.environmentId, selectedThread.id)
257+
: null;
258+
if (backgroundStopRequest !== null && backgroundStopRequest.threadKey !== backgroundThreadKey) {
259+
setBackgroundStopRequest(null);
260+
}
252261
const loadEarlierHistory = useAtomCommand(threadEnvironment.loadEarlierHistory, {
253262
label: "load earlier thread history",
254263
reportFailure: false,
@@ -597,6 +606,26 @@ function ThreadRouteContent(
597606
});
598607
}, [composer.interruptibleRunId, interruptThreadTurn, selectedThread]);
599608

609+
const handleStopBackgroundWork = useCallback(async () => {
610+
if (
611+
!selectedThread ||
612+
!readEnvironmentScope(selectedThread.environmentId, AuthOrchestrationOperateScope)
613+
) {
614+
return;
615+
}
616+
const request = {
617+
threadKey: scopedThreadKey(selectedThread.environmentId, selectedThread.id),
618+
};
619+
setBackgroundStopRequest(request);
620+
await interruptThreadTurn({
621+
environmentId: selectedThread.environmentId,
622+
input: { threadId: selectedThread.id },
623+
});
624+
// Acceptance does not confirm termination. Allow retry while tasks remain,
625+
// including when the interrupt fails, matching the web client's behavior.
626+
setBackgroundStopRequest((current) => (current === request ? null : current));
627+
}, [interruptThreadTurn, selectedThread]);
628+
600629
const handleOpenTerminal = useCallback(
601630
(nextTerminalId?: string | null) => {
602631
terminalDebugLog("terminal-menu:open-existing", {
@@ -1008,6 +1037,8 @@ function ThreadRouteContent(
10081037
onRemoveDraftImage={composer.onRemoveDraftImage}
10091038
serverConfig={serverConfig}
10101039
onStopThread={awaitingBootstrapTurn ? handleCancelWorktreeSetup : handleStopThread}
1040+
stoppingBackgroundWork={backgroundStopRequest !== null}
1041+
onStopBackgroundWork={handleStopBackgroundWork}
10111042
onSendMessage={composer.onSendMessage}
10121043
onReconnectEnvironment={handleReconnectEnvironment}
10131044
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
@@ -68,6 +68,8 @@ export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTRO
6868
export function FloatingWorkingControl(props: {
6969
readonly colorScheme: "light" | "dark";
7070
readonly status: FloatingWorkingStatus | null;
71+
/** Stop for background work that outlived its turn, beside the label naming it. */
72+
readonly backgroundStop: { readonly stopping: boolean; readonly onPress: () => void } | null;
7173
readonly devicePreview: { readonly count: number; readonly onPress: () => void } | null;
7274
readonly browserPreview: { readonly count: number; readonly onPress: () => void } | null;
7375
readonly showScrollToEnd: boolean;
@@ -83,6 +85,9 @@ export function FloatingWorkingControl(props: {
8385
const [overlayWidth, setOverlayWidth] = useState(windowWidth);
8486
const [queueWidth, setQueueWidth] = useState(0);
8587
const [agentsWidth, setAgentsWidth] = useState(0);
88+
const [stopWidth, setStopWidth] = useState(0);
89+
const backgroundStop = props.status?.kind === "background" ? props.backgroundStop : null;
90+
const hasStop = backgroundStop !== null;
8691
const hasQueue = props.queuedCount > 0;
8792
const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
8893
const [previewWidth, setPreviewWidth] = useState(0);
@@ -95,6 +100,7 @@ export function FloatingWorkingControl(props: {
95100
Math.min(overlayWidth, windowWidth) -
96101
CONTROL_HEIGHT -
97102
32 -
103+
(hasStop ? stopWidth : 0) -
98104
(hasQueue ? queueWidth : 0) -
99105
(hasAgents ? agentsWidth : 0) -
100106
(hasPreview ? previewWidth : 0),
@@ -160,9 +166,9 @@ export function FloatingWorkingControl(props: {
160166
hasAgents ||
161167
hasQueue ||
162168
(props.devicePreview !== null && props.browserPreview !== null);
163-
// The queue, agents, and reconnect labels have separate tap targets.
169+
// The stop, queue, agents, and reconnect labels have separate tap targets.
164170
const statusInteractive = props.status?.kind === "connection";
165-
const capsuleInteractive = statusInteractive || hasQueue || hasAgents || hasPreview;
171+
const capsuleInteractive = statusInteractive || hasStop || hasQueue || hasAgents || hasPreview;
166172
// The host stays centered on the capsule, but its measurement constraint
167173
// comes from the overlay, independent of the capsule's current width.
168174
const statusContent =
@@ -193,6 +199,21 @@ export function FloatingWorkingControl(props: {
193199
const capsuleContent = (
194200
<View className="flex-row items-center">
195201
{statusContent}
202+
{backgroundStop !== null ? (
203+
<Pressable
204+
accessibilityRole="button"
205+
accessibilityLabel={backgroundStop.stopping ? "Stopping" : "Stop background work"}
206+
disabled={backgroundStop.stopping}
207+
onPress={backgroundStop.onPress}
208+
onLayout={(event) => setStopWidth(event.nativeEvent.layout.width)}
209+
className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
210+
>
211+
<View className="mr-1 h-4 w-px bg-border" />
212+
<Text className="font-t3-medium text-xs text-danger-foreground" numberOfLines={1}>
213+
{backgroundStop.stopping ? "Stopping…" : "Stop"}
214+
</Text>
215+
</Pressable>
216+
) : null}
196217
{hasPreview ? (
197218
<View
198219
className="h-11 flex-row items-center"

0 commit comments

Comments
 (0)